Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Intl.NumberFormat vs RegExp
(version: 0)
Comparing performance of:
Intl.NumberFormat vs toLocalString
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const formatNumberToFloat = new Intl.NumberFormat('ru-RU', { style: 'decimal', maximumFractionDigits: 2, minimumFractionDigits: 2, })
Tests:
Intl.NumberFormat
const formatNumberToFloat = new Intl.NumberFormat('ru-RU', { style: 'decimal', maximumFractionDigits: 2, minimumFractionDigits: 2, }) var a = formatNumberToFloat.format("10000.999999");
toLocalString
var a = (Math.round( "10000.999999" * 100 + Number.EPSILON ) / 100).toFixed(2); var b = a.replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1 ')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Intl.NumberFormat
toLocalString
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0
Browser/OS:
Firefox 121 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Intl.NumberFormat
36439.8 Ops/sec
toLocalString
3229272.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **Overview** The benchmark compares two approaches to formatting numbers: using the `Intl.NumberFormat` API and regular expressions (RegExp). The goal is to determine which approach is faster for this specific use case. ** Intl.NumberFormat API** The `Intl.NumberFormat` API is a built-in JavaScript API that provides support for formatting numbers according to the rules of different cultures. In this benchmark, we're using it to format a Russian decimal number with two fractional digits. **Script Preparation Code** The script preparation code creates an instance of `Intl.NumberFormat` with specific settings: ```javascript const formatNumberToFloat = new Intl.NumberFormat('ru-RU', { style: 'decimal', maximumFractionDigits: 2, minimumFractionDigits: 2, }); ``` This code sets the formatting style to decimal, and limits the number of fractional digits to 2. **Test Case** The test case consists of two parts: 1. The first part defines a variable `a` using the `formatNumberToFloat` instance: ```javascript var a = formatNumberToFloat.format("10000.999999"); ``` This code formats the string "10000.999999" according to the Russian decimal rules, resulting in a formatted number. 2. The second part defines another variable `a` using regular expressions: ```javascript var b = (Math.round("10000.999999" * 100 + Number.EPSILON) / 100).toFixed(2); var c = b.replace(/(\\d)(?=(\\d{3})+(?!\\d))/g, '$1 '); ``` This code uses regular expressions to extract every third digit from the number and append a space after each one. **Pros and Cons** Using `Intl.NumberFormat` has several advantages: * It's a built-in API, so it's likely to be fast and efficient. * It provides accurate formatting according to specific cultural rules. * It eliminates the need for manual implementation of decimal rules. However, using `Intl.NumberFormat` also has some limitations: * It may not work correctly in older browsers or environments that don't support this API. * The performance can vary depending on the browser and its locale settings. On the other hand, regular expressions (RegExp) have the following advantages: * They are widely supported across most browsers and environments. * They provide a high degree of control over formatting. * However, they require manual implementation of decimal rules, which can be error-prone and slow. However, using RegExp also has some disadvantages: * It requires more code and is potentially slower than `Intl.NumberFormat`. * The performance can vary depending on the browser and its locale settings. **Other Considerations** Another consideration is that this benchmark only measures the execution time of formatting a single number. In real-world scenarios, you may need to format multiple numbers, which could impact the overall performance. Additionally, if you're using `Intl.NumberFormat`, make sure to handle errors and exceptions properly, as it can throw errors for invalid input or unsupported locales. **Alternatives** If you don't want to use `Intl.NumberFormat`, alternative approaches include: * Using a third-party library like moment.js or date-fns. * Implementing decimal rules manually using regular expressions or mathematical calculations. * Using a different formatting API, such as the one provided by the Google Closure Library. However, these alternatives may have their own trade-offs in terms of performance, accuracy, and maintainability.
Related benchmarks:
New NumberFormat vs ExistingNumberFormat
Number format
Using Intl NumberFormatter vs String manipulation
new Intl.NumberFormat vs reused NumberFormat reformatted
Comments
Confirm delete:
Do you really want to delete benchmark?