<script> window.formatter = new Intl.NumberFormat("en-US",{
style: 'decimal',
useGrouping: true,
minimumFractionDigits: 0,
maximumFractionDigits: 20,
});</script>
var a = formatter.format("10000000.1234");
var a = "10000000.1234".toLocaleString("en-US",{
style: 'decimal',
useGrouping: true,
minimumFractionDigits: 0,
maximumFractionDigits: 20,
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Intl.NumberFormat | |
toLocalString |
Test name | Executions per second |
---|---|
Intl.NumberFormat | 845591.1 Ops/sec |
toLocalString | 14903486.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
What is being tested?
The benchmark tests two approaches for formatting numbers with grouping and decimal places:
Intl.NumberFormat
(a built-in JavaScript API)toLocaleString
method without the Intl.NumberFormat
API (i.e., relying on the browser's default number formatting behavior)Options being compared
The benchmark compares two options:
Intl.NumberFormat
toLocaleString
without Intl.NumberFormat
Library: Intl.NumberFormat
The Intl.NumberFormat
library is a part of the Internationalization (i18n) API in JavaScript. It provides a standardized way to format numbers according to regional conventions, ensuring that dates and numbers are displayed correctly for different languages and locales.
In this benchmark, Intl.NumberFormat
is used with specific options to format numbers with grouping and decimal places. The library takes into account the specified locale (in this case, "en-US") and formatting options (e.g., useGrouping, minimumFractionDigits, maximumFractionDigits).
Special JS feature: none mentioned
There are no special JavaScript features or syntax being used in this benchmark.
Other alternatives
If you were to write a similar benchmark for other number formatting approaches, some alternative options could be:
NumberFormat
from a third-party library (e.g., moment.js)Keep in mind that these alternatives might not provide the same level of consistency and standardization as Intl.NumberFormat
.