Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
number format3
(version: 3)
Comparing performance of:
C1 vs S1 vs C2 vs S2 vs C3 vs S3 vs INTL1
Created:
2 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/9.0.1/bignumber.min.js"></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
function _numberWithCommas(strNum) { return strNum.replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ","); } function numberFloorFormat (num, decimal, roundMode, fillingZero) { let rounded; if (roundMode === 'up') { rounded = _.ceil(num, decimal); } else if (roundMode === 'down') { rounded = _.floor(num, decimal); } let strNum; if (fillingZero) { strNum = rounded.toFixed(decimal); } else { strNum = rounded.toString(); } return _numberWithCommas(strNum); }; var intlFormat = Intl.NumberFormat({roundingMode: 'floor', maximumFractionDigits: 8}).format;
Tests:
C1
new BigNumber(12345678.123456).toFormat(8, BigNumber.ROUND_FLOOR)
S1
numberFloorFormat(12345678.123456, 8, 'down', true)
C2
new BigNumber(12345678.123456).toFormat(8, BigNumber.ROUND_CEIL)
S2
numberFloorFormat(12345678.123456, 8, 'up', true)
C3
new BigNumber(12345678.123456).dp(8, BigNumber.ROUND_CEIL).toFormat()
S3
numberFloorFormat(12345678.123456, 8, 'up', false)
INTL1
intlFormat(12345678.123456)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (7)
Previous results
Fork
Test case name
Result
C1
S1
C2
S2
C3
S3
INTL1
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark created using MeasureThat.net. The benchmark tests the performance of different approaches to format numbers with commas, rounding modes, and decimal places. **Benchmark Definitions** There are six individual test cases: 1. `C1`: Tests the use of `new BigNumber(12345678.123456).toFormat(8, BigNumber.ROUND_FLOOR)`, which uses the `BigNumber` library to perform arithmetic operations. 2. `S1`: Tests the implementation of the `numberFloorFormat` function, which is a custom solution for formatting numbers with commas and rounding modes. 3. `C2`: Similar to `C1`, but uses `new BigNumber(12345678.123456).toFormat(8, BigNumber.ROUND_CEIL)`. 4. `S2`: Similar to `S1`, but uses `numberFloorFormat` with the `up` rounding mode. 5. `C3`: Tests the use of `new BigNumber(12345678.123456).dp(8, BigNumber.ROUND_CEIL).toFormat()`, which uses the `BigNumber` library for decimal places. 6. `INTL1`: Tests the use of the `Intl.NumberFormat` API to format numbers with commas and decimal places. **Options Compared** The benchmark compares different approaches: * **Custom solution (`numberFloorFormat`)**: Uses a custom implementation to format numbers, which allows for more control over rounding modes and decimal places. + Pros: More flexible, can handle edge cases not covered by the `BigNumber` library. + Cons: May be less efficient than using established libraries like `BigNumber`. * **`BigNumber` library**: Uses the `BigNumber` library to perform arithmetic operations, which provides a simple and efficient way to work with large numbers. + Pros: Fast, reliable, and widely supported. + Cons: Limited control over rounding modes and decimal places. * **`Intl.NumberFormat` API**: Uses the internationalization API to format numbers, which provides a flexible and accurate way to handle different number formats and locale-specific settings. + Pros: Accurate, handles edge cases well, and supports multiple locales. + Cons: May be slower than custom solutions or `BigNumber`, and requires more setup. **Other Considerations** * **Performance**: The benchmark prioritizes performance, so the results are focused on execution time rather than code quality or readability. * **Libraries used**: The benchmark uses the `BigNumber` library for arithmetic operations and the `Intl.NumberFormat` API for formatting numbers. * **Rounding modes**: The benchmark includes tests with both `ROUND_FLOOR` and `ROUND_CEIL` rounding modes to evaluate their performance. **Conclusion** The benchmark provides a comprehensive evaluation of different approaches to formatting numbers in JavaScript, including custom solutions, established libraries like `BigNumber`, and the internationalization API (`Intl.NumberFormat`). By comparing these options, developers can choose the best approach for their specific use case, taking into account factors such as performance, flexibility, and accuracy.
Related benchmarks:
Decimal rounding
Using Intl NumberFormatter vs String manipulation
number format2
Formatting number, including NaN
Comments
Confirm delete:
Do you really want to delete benchmark?