Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
checking 2 fun locale array creation
(version: 0)
Comparing performance of:
Original vs Improved one
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var NumberFormatUtils = { format: (locale, number) => number.toString(), formatToParts: (locale, number) => [ { type: 'decimal', value: '.' }, { type: 'minusSign', value: '-' }, { type: 'group', value: ',' } ] }; var STANDARD_DIGITS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '-', ',']; var INDEX_DECIMAL = 10; var INDEX_MINUS_SIGN = 11; var INDEX_GROUP = 12; var LOCALE_TEST = 'en-US'; // Example locale
Tests:
Original
const getLocaleDigitsOriginal = (locale) => { const localeDigits = [...STANDARD_DIGITS]; for (let i = 0; i <= 9; i++) { localeDigits[i] = NumberFormatUtils.format(locale, i); } NumberFormatUtils.formatToParts(locale, 1000000.5).forEach((part) => { switch (part.type) { case 'decimal': localeDigits[INDEX_DECIMAL] = part.value; break; case 'minusSign': localeDigits[INDEX_MINUS_SIGN] = part.value; break; case 'group': localeDigits[INDEX_GROUP] = part.value; break; default: break; } }); return localeDigits; }; getLocaleDigitsOriginal(LOCALE_TEST);
Improved one
const DIGITS = [INDEX_DECIMAL, INDEX_MINUS_SIGN, INDEX_GROUP]; const getLocaleDigitsOptimized = (locale) => { const localeDigits = []; // Set standard digits for (let i = 0; i <= 9; i++) { localeDigits[i] = NumberFormatUtils.format(locale, i); } // Set standard symbols (decimal, minus sign, group) DIGITS.forEach((index) => { localeDigits[index] = STANDARD_DIGITS[index]; }); // Update locale-specific symbols NumberFormatUtils.formatToParts(locale, 1000000.5).forEach((part) => { switch (part.type) { case 'decimal': localeDigits[INDEX_DECIMAL] = part.value; break; case 'minusSign': localeDigits[INDEX_MINUS_SIGN] = part.value; break; case 'group': localeDigits[INDEX_GROUP] = part.value; break; default: break; } }); return localeDigits; }; getLocaleDigitsOptimized(LOCALE_TEST);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Original
Improved one
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Original
1457358.9 Ops/sec
Improved one
1030006.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. The provided benchmark tests two approaches to locale-dependent digit creation in JavaScript, specifically for the English (US) locale. **Original Approach** In the original approach, the `getLocaleDigitsOriginal` function iterates over the standard digits (0-9) and formats each one using `NumberFormatUtils.format(locale, i)`. This creates a new array with formatted digits. Additionally, it extracts the decimal, minus sign, and group symbols from `NumberFormatUtils.formatToParts(locale, 1000000.5)` and updates the corresponding positions in the digit array. **Improved Approach** The improved approach uses an optimized version of the same logic but with some key differences: * It pre-sets the standard digits and symbols in a separate array (`STANDARD_DIGITS` and `DIGITS`, respectively). * It sets these pre-defined values for the decimal, minus sign, and group positions directly. * Finally, it updates the locale-specific symbols from `NumberFormatUtils.formatToParts(locale, 1000000.5)`. **Pros and Cons** * **Original Approach:** + Pros: - Easy to understand and implement. - Uses the full capabilities of `NumberFormatUtils`. + Cons: - May be slower due to the additional formatting steps. * **Improved Approach:** + Pros: - Optimized by reusing pre-defined values for standard digits and symbols. - Faster execution since fewer function calls are required. + Cons: - Less readable due to the use of magic numbers and direct assignments. **Other Considerations** When working with locale-dependent formatting in JavaScript, it's essential to consider the following: * The `NumberFormatUtils` library is a built-in part of the ECMAScript standard (ECMAScript 402) and provides consistent formatting across browsers. * When using this library, be aware that its behavior might vary slightly between browsers due to implementation differences. **Library: NumberFormatUtils** The `NumberFormatUtils` library is a set of utility functions for working with numeric values in JavaScript. Its primary purpose is to provide a standardized way of formatting numbers and extracting their parts (decimal, minus sign, group, etc.). This library is implemented as part of the ECMAScript standard. **Special JS Features/Syntax** None mentioned in this benchmark. **Alternatives** If you need to create locale-dependent digit arrays or perform similar formatting tasks, consider using alternative libraries or approaches: * `Intl.NumberFormat` API: A more modern and widely supported way of working with numeric values and locales. * Custom implementations: Depending on your specific requirements, creating a custom implementation might be more efficient than relying on the `NumberFormatUtils` library. When optimizing performance-critical code, it's essential to weigh the trade-offs between readability, maintainability, and execution speed. The improved approach in this benchmark offers a balance between these factors but may require additional testing to confirm its performance benefits in all scenarios.
Related benchmarks:
Intl.NumberFormat vs toLocalString 2
Intl.NumberFormat vs toLocalString vs Custom Format vs Pre-created Intl formatter
Intl.NumberFormat vs toLocaleString vs Custom Formatter vs Pre-created Intl formatter
Intl.NumberFormat vs toLocalString vs Intl.NumberFormat instantiated
Comments
Confirm delete:
Do you really want to delete benchmark?