JavaScript · Senior · Question 43 of 75

Why cache Intl.NumberFormat instances?

Explanation

Intl constructors load locale data and parse options. Calling toLocaleString() in a loop over thousands of rows creates a formatter every time. Better to create new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' }) once and call format.

Further reading

Problem with a question

JavaScript · Senior · question 43

Test yourself

Take the JavaScript · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz