Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Intl.NumberFormat vs toLocalString vs string split vs toFixed vs bignumber vs regex
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:122.0) Gecko/20100101 Firefox/122.0
Browser:
Firefox 122
Operating system:
Linux
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
toLocaleString
24530.9 Ops/sec
Intl.NumberFormat
24918.7 Ops/sec
string split & reduce
2041439.1 Ops/sec
toFixed
10548373.0 Ops/sec
bignumber
507128.2 Ops/sec
numberWithCommas
1467026.8 Ops/sec
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/9.0.1/bignumber.min.js"></script>
Script Preparation code:
function pricize (num, decimal = 0) { const intPart = Math.floor(num) const intPieces = intPart.toString().split('') const len = intPieces.length const intStr = intPieces.reduce((result, chr, index, array) => { const pos = len - index result += (index !== 0 && pos % 3 === 0) ? ',' + chr : chr return result }, '') const decStr = decimal > 0 ? num.toFixed(decimal + 1).toString().slice(-(decimal + 2), -1) // toFixed() は四捨五入してしまうので(decimal + 1)桁まで求めて切り取る : '' return intStr + decStr } function numberWithCommas(x) { return x.replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ","); } var target = 123456789.123456789 var options = {minimumFractionDigits: 3, maximumFractionDigits: 3}
Tests:
toLocaleString
var a = target.toLocaleString('en-US', options);
Intl.NumberFormat
var a = new Intl.NumberFormat('en-US', options).format(target)
string split & reduce
var a = pricize(target, 3)
toFixed
var a = target.toFixed(3)
bignumber
var a = new BigNumber(target).toFormat(3)
numberWithCommas
var a = numberWithCommas(target.toFixed(3))