toFixed(4) someFloat.toFixed(4); someFloat.t vs toPrecision(4).toString() vs (Math.round(*10000)/10000).toString() vs Math.floor fast vs Math.truncFastest: Math.floor fast (4.4x faster)
29,809,358 ops/sec
11,224,034 ops/sec
10,888,918 ops/sec
7,656,532 ops/sec
6,833,404 ops/sec
Fastest: (Math.round(*10000)/10000).toString() (121.3x faster)
1,582,026,112 ops/sec
1,562,475,776 ops/sec
1,454,891,776 ops/sec
18,130,968 ops/sec
13,039,024 ops/sec
Results show median performance across all community submissions. Run this benchmark yourself to verify on your device.
| Test case name | Result |
|---|---|
| toFixed(4) someFloat.toFixed(4); someFloat.t | |
| toPrecision(4).toString() | |
| (Math.round(*10000)/10000).toString() | |
| Math.floor fast | |
| Math.trunc |
| Test name | Executions per second |
|---|---|
| ✓ Math.floor fast | 130,093,736 Ops/sec |
| (Math.round(*10000)/10000).toString() | 127,316,816 Ops/sec |
| Math.trunc | 121,926,080 Ops/sec |
| toPrecision(4).toString() | 16,568,263 Ops/sec |
| toFixed(4) someFloat.toFixed(4); someFloat.t | 10,964,273 Ops/sec |