var someFloat = 0.123456789;
someFloat.toFixed(4);
someFloat.toPrecision(4);
(Math.round(someFloat*10000)/10000);
~~(someFloat * 10000) / 10000;
Math.trunc(someFloat * 10000) / 10000;
--enable-precise-memory-info
flag.
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 |
---|---|
toFixed(4) someFloat.toFixed(4); someFloat.t | 19306932.0 Ops/sec |
toPrecision(4).toString() | 31942058.0 Ops/sec |
(Math.round(*10000)/10000).toString() | 170359776.0 Ops/sec |
Math.floor fast | 182606352.0 Ops/sec |
Math.trunc | 176509504.0 Ops/sec |
Let's break down what's being tested in the provided JSON.
Benchmark Definition
The benchmark is testing the performance of different methods to round or truncate a floating-point number:
toFixed(4)
: This method rounds the number to 4 decimal places using the "fixed" rounding mode.toPrecision(4).toString()
: This method uses the toPrecision()
method to specify the precision and then converts the result to a string.(Math.round(*10000)/10000).toString()
: This method multiplies the number by 10,000, rounds the result using Math.round()
, and then divides by 10,000 before converting to a string.~~(someFloat * 10000) / 10000
: This method uses bitwise NOT (~
) to effectively truncate the number (more on this later).Math.trunc(someFloat * 10000) / 10000
: This method uses the new Math.trunc()
function (introduced in ECMAScript 2020) to truncate the number.Options Compared
The benchmark is comparing these five different methods:
toFixed(4)
vs toPrecision(4).toString()
: These two methods use different rounding modes, but both aim to achieve a similar result.(Math.round(*10000)/10000).toString()
vs ~~(someFloat * 10000) / 10000
: Both methods use bitwise operations or multiplication/division by large numbers to truncate the number.(Math.round(*10000)/10000).toString()
vs Math.trunc(someFloat * 10000) / 10000
: The first method uses a more traditional rounding approach, while the second method uses the new Math.trunc()
function.Pros and Cons
Here's a brief summary of each method:
toFixed(4)
:toPrecision(4).toString()
:toFixed(4)
, but may be slower due to the extra conversion step.toPrecision()
method.(Math.round(*10000)/10000).toString()
:~~(someFloat * 10000) / 10000
:Math.trunc(someFloat * 10000) / 10000
:~~(someFloat * 10000) / 10000
, and easier to read for some users.Library and Special JS Features
The benchmark uses the following library:
Math.trunc()
is a new function introduced in ECMAScript 2020, which provides an efficient way to truncate numbers without using bitwise operations.If any special JavaScript features are used, they might be implicit in the ~~(someFloat * 10000) / 10000
method, as it relies on the ~
operator (bitwise NOT). This feature was introduced in ECMAScript 2015.
Alternatives
Other alternatives for rounding or truncating numbers include:
lodash
or moment.js
, which provide more complex and powerful rounding functions.~~(someFloat * 10000) / 10000
method.Keep in mind that the choice of rounding method depends on the specific use case and requirements.