Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Formatting number, including NaN
(version: 1)
Formatting a float number
Comparing performance of:
OldFormatNumber vs NewFormatNumber vs FormatNumber for NaN (both)
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
function newFormatNumber(value, decimals, decimalSeparator = ',', thousandsSeparator = '.', prefix = '', suffix = '') { if (value === null || isNaN(value)) return value; let negative = value < 0 ? '-' : ''; let absValue = Math.abs(value); let integerPart = Math.floor(absValue); let decimalPart = absValue - integerPart; let formattedIntegerPart = integerPart.toString().replace(/\B(?=(\d{3})+(?!\d))/g, thousandsSeparator); let formattedDecimalPart = decimalPart.toFixed(decimals).slice(2); return `${prefix}${negative}${formattedIntegerPart}${decimalSeparator}${formattedDecimalPart}${suffix}`; } function oldFormatNumber(value, options = {}) { if (value === null || isNaN(value)) return value; let standardOptions = { minimumFractionDigits: (typeof options.minimumFractionDigits === "number" ? options.minimumFractionDigits : null ) || (typeof options.fractionDigits === "number" ? options.fractionDigits : 2), maximumFractionDigits: (typeof options.maximumFractionDigits === "number" ? options.maximumFractionDigits : null ) || (typeof options.fractionDigits === "number" ? options.fractionDigits : 2), } return (value * 1).toLocaleString('ro', standardOptions); }
Tests:
OldFormatNumber
let num = oldFormatNumber(-123456.7890123, {fractionDigits: 2});
NewFormatNumber
let num = newFormatNumber(-123456.7890123, 2, ',', '.', '', ' EUR');
FormatNumber for NaN (both)
let num = oldFormatNumber('abc', {fractionDigits: 2});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
OldFormatNumber
NewFormatNumber
FormatNumber for NaN (both)
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/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
OldFormatNumber
114542.9 Ops/sec
NewFormatNumber
2819813.8 Ops/sec
FormatNumber for NaN (both)
9816589.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Definition** The benchmark measures the performance of two functions, `oldFormatNumber` and `newFormatNumber`, which are used to format numbers with decimal places. The formatting rules include: * Negative sign * Thousands separator (optional) * Decimal place **Options Compared** The benchmark compares the performance of two approaches: 1. **oldFormatNumber**: This function uses the `toLocaleString` method provided by the Web API, which is part of the HTML5 specification. It takes an options object with `minimumFractionDigits` and `maximumFractionDigits` properties to control the number of decimal places. 2. **newFormatNumber**: This function is a custom implementation that manually handles the formatting logic. **Pros and Cons** **oldFormatNumber**: Pros: * Easy to implement * Uses native Web API, which is likely optimized for performance Cons: * Limited control over formatting options (e.g., thousands separator) * May not work correctly with all input types or cultures **newFormatNumber**: Pros: * Offers more control over formatting options * Can handle custom formatting rules and input types Cons: * Requires manual implementation, which can be error-prone * May have performance implications due to the extra logic **Library Used** The `toLocaleString` method is part of the HTML5 Web API and is implemented by web browsers. It provides a standardized way to format numbers and dates in a culturally aware manner. **Special JS Feature or Syntax** There are no special JavaScript features or syntax used in this benchmark, apart from using arrow functions ( implicit return) which is a relatively modern feature in ECMAScript 2015+. **Other Alternatives** If you wanted to implement the formatting logic manually without using `toLocaleString`, you could use string manipulation techniques, such as: * Using regular expressions to extract and format the decimal parts * Using string concatenation or template literals to build the formatted string However, these approaches would likely be slower and less efficient than using the native Web API. In summary, the benchmark measures the performance of two functions for formatting numbers with decimal places. The `oldFormatNumber` function uses the `toLocaleString` method, which is a standardized approach, while the `newFormatNumber` function provides more control over formatting options but requires manual implementation.
Related benchmarks:
Using Intl NumberFormatter vs String manipulation
number format3
Teste Truncar numero
check Replace on number format
Comments
Confirm delete:
Do you really want to delete benchmark?