Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Formatting number, including NaN
Formatting a float number
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
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:
Chrome 129
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
OldFormatNumber
114542.9 Ops/sec
NewFormatNumber
2819813.8 Ops/sec
FormatNumber for NaN (both)
9816589.0 Ops/sec
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});