Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Rounding methods
First test is incorrect but needed to evaluate the performance penalty
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser:
Firefox 140
Operating system:
Windows
Device Platform:
Desktop
Date tested:
one year ago
Test name
Executions per second
Simple rounding
772902656.0 Ops/sec
Rounding with epsilon and string conversion
3671393.0 Ops/sec
Rounding with "if" statement
26372596.0 Ops/sec
Rounding by toLocaleString
48457.8 Ops/sec
Script Preparation code:
function round2(num, places) { return +(Math.round(num + "e+" + places) + "e-" + places); } function round3(num, places) { if (places === 2) { return Math.round( Math.round( num * 1000 ) / 10 ) / 100 } else { const multiplier = Math.pow(10, places) return Math.round(num * multiplier) / multiplier } } function round4(num, places) { +(num.toLocaleString( 'en', { maximumFractionDigits: places, useGrouping: false } )) }
Tests:
Simple rounding
Math.round(12.345 * 100) / 100
Rounding with epsilon and string conversion
round2(12.345, 2)
Rounding with "if" statement
round3(12.345, 2)
Rounding by toLocaleString
round4(12.345, 2)