Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36
Chrome 103
Mac OS X 10.11.6
Desktop
one year ago
Math.floor fast
157.6M/s
Math.trunc
61.0M/s
(Math.round(*10000)/10000).toString()
52.9M/s
toPrecision(4).toString()
2.9M/s
toFixed(4) someFloat.toFixed(4); someFloat.t
2.8M/s
View exact numbers
Test name Executions per second
Math.floor fast 157,551,488 Ops/sec
Math.trunc 60,952,100 Ops/sec
(Math.round(*10000)/10000).toString() 52,948,840 Ops/sec
toPrecision(4).toString() 2,926,044 Ops/sec
toFixed(4) someFloat.toFixed(4); someFloat.t 2,764,340 Ops/sec
Script Preparation code:
Tests: