Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Number.toLocaleString vs Intl.NumberFormat vs Regex
(version: 0)
Comparing performance of:
Intl.NumberFormat vs Number.toLocaleString vs Regex
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Intl.NumberFormat
const currencyPlayNumber = 1; const translatedString = new Intl.NumberFormat(undefined, { style: 'currency', currency: 'RUB', }).format(currencyPlayNumber);
Number.toLocaleString
const currencyPlayNumber = 1; const translatedString = currencyPlayNumber.toLocaleString(undefined, { style: 'currency', currency: 'RUB', })
Regex
const currencyPlayNumber = 1; const translatedString = `${currencyPlayNumber}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Intl.NumberFormat
Number.toLocaleString
Regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36 Edg/145.0.0.0
Browser/OS:
Chrome 145 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Intl.NumberFormat
11946.2 Ops/sec
Number.toLocaleString
15220.2 Ops/sec
Regex
5762923.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark measures the performance of three different approaches to format a currency string: `Intl.NumberFormat`, `Number.toLocaleString`, and regular expression (Regex). The goal is to determine which approach is the most efficient. **Options Compared** 1. **Intl.NumberFormat**: This method uses the Internationalization API to format numbers according to a specific locale, in this case, Russian (RUB). 2. **Number.toLocaleString**: This method uses the `toLocaleString` method of the JavaScript number object to format numbers according to a specific locale. 3. **Regex**: This method uses regular expressions to manually replace digits with commas, creating a currency string. **Pros and Cons** * **Intl.NumberFormat**: + Pros: widely supported by modern browsers, flexible locale options, handles formatting for multiple currencies and number formats. + Cons: may have performance overhead due to internationalization considerations. * **Number.toLocaleString**: + Pros: widely supported by modern browsers, efficient performance, simple implementation. + Cons: limited locale options compared to `Intl.NumberFormat`. * **Regex**: + Pros: lightweight and flexible, can handle formatting for multiple currencies and number formats. + Cons: requires manual implementation, potential performance overhead due to string manipulation. **Library/Technique Used** None of the above methods rely on external libraries. However, the use of internationalization APIs (`Intl.NumberFormat`) may be considered a technique or library in itself, as it provides a standardized way to handle formatting for different locales and currencies. **Special JS Feature/Syntax** No special JavaScript features or syntax are used in these benchmarks. **Other Alternatives** If none of the above methods are suitable, alternative approaches could include: 1. Using a dedicated JavaScript library for internationalization, such as Moment.js. 2. Implementing custom formatting logic using a template engine like Handlebars. 3. Using a CSS-based approach to format numbers, such as by manipulating the HTML element's style attribute. These alternatives may offer better performance or flexibility but also introduce additional complexity and overhead.
Related benchmarks:
Intl.NumberFormat vs toLocalString
Intl.NumberFormat vs toLocalString 2
Intl.NumberFormat vs toLocalString ( predefine the NumberFormat instance )
cached Intl.NumberFormat vs toLocalString
Intl.NumberFormat vs toLocalString yoyo
Comments
Confirm delete:
Do you really want to delete benchmark?