Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
number format2
(version: 1)
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); }; function intlFormat() { return Intl.NumberFormat({roundingMode: 'floor', maximumFractionDigits: 8}); }
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):
Let's break down what is tested on the provided JSON and explain the different options compared. **Benchmark Definition** The benchmark definition is a JavaScript function that formats a number as a string with commas. There are four main functions being tested: 1. `numberFloorFormat`: This function takes a number, decimal places, round mode (up or down), and filling zero as input. It returns the formatted string. 2. `intlFormat`: This function uses the Internationalization API to format the number as a string with commas. 3. `_numberWithCommas`: This is a helper function that replaces the occurrences of `\d{3}` in the string with commas. **Options Compared** The four functions are compared on the following options: * **Round mode**: `up` or `down`. The `up` mode rounds up to the nearest whole number, while `down` rounds down. * **Filling zero**: `true` or `false`. If `true`, zeros are added to the left of the decimal point if necessary. **Pros and Cons** Here's a brief summary of each option: 1. **Number Floor Format (`numberFloorFormat`)**: * Pros: Can be customized with round mode and filling zero options. * Cons: May have performance issues due to string manipulation. 2. **Intl Format (`intlFormat`)**: * Pros: Uses the Internationalization API, which is more accurate for formatting numbers. * Cons: May not work correctly in older browsers or environments that don't support Intl. 3. `_numberWithCommas`**: * Pros: Simple and lightweight. * Cons: Limited customization options. **Library: Bignumber.js** The `Bignumber.js` library is used in the benchmark definition to perform large number arithmetic. It's a popular library for working with big numbers in JavaScript. **Special JS Feature/Syntax** The benchmark definition uses some special features and syntax, such as: * `_`: The underscore function is imported from Lodash. * `BigNumber`: The BigNumber class is used for large number arithmetic. * `ROUND_FLOOR` and `ROUND_CEIL`: These are constants defined in the BigNumber.js library. **Alternatives** Some alternative libraries or approaches could be used instead of Bignumber.js: * GMP (Greatest Common Divisor) libraries like Gmplib or mpmath * JavaScript libraries for big number arithmetic, such as jbn or BigNum Keep in mind that these alternatives might have different performance characteristics and may require additional setup. I hope this explanation helps you understand the benchmark definition and the options being compared!
Related benchmarks:
Decimal rounding
padding right zero
number format3
Formatting number, including NaN
Comments
Confirm delete:
Do you really want to delete benchmark?