var someFloat = 0.123456789;
someFloat.toFixed(4);
someFloat.toPrecision(4);
~(someFloat*1000)/-1000
~~(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() | |
~(someFloat*1000)/-1000 | |
Math.floor fast | |
Math.trunc |
Test name | Executions per second |
---|---|
toFixed(4) someFloat.toFixed(4); someFloat.t | 10640115.0 Ops/sec |
toPrecision(4).toString() | 9127618.0 Ops/sec |
~(someFloat*1000)/-1000 | 25795182.0 Ops/sec |
Math.floor fast | 25780622.0 Ops/sec |
Math.trunc | 10552778.0 Ops/sec |
Let's break down the provided benchmark JSON and explain what is being tested.
Benchmark Definition
The test compares the performance of five different methods for rounding or truncating numbers:
toFixed(4)
: Rounds a number to 4 decimal places.toPrecision(4).toString()
: Converts a number to a string with a precision of 4 digits.(~~Math.round(*10000)).toString()
: Uses the bitwise NOT operator (~
) to round down a large number to the nearest integer, and then converts it to a string.Math.floor fast
: A shortened version of Math.floor()
that is optimized for performance.Math.trunc(someFloat * 10000) / 10000
: Uses the Math.trunc()
function to truncate a large number.Options compared
Each option has its pros and cons:
toFixed(4)
and toPrecision(4).toString()
: These methods are simple and easy to understand, but may not be as efficient for large numbers. They also require more CPU cycles than some of the other options.(~~Math.round(*10000)).toString()
: This method uses a bitwise NOT operator to round down the number, which can be slower than other methods. However, it is often used in JavaScript because it is simple and works well for many cases.Math.floor fast
: As mentioned earlier, this is an optimized version of Math.floor()
. It is likely faster than the other options but may require more complex code to implement.Math.trunc(someFloat * 10000) / 10000
: This method uses Math.trunc()
to truncate the number, which can be slower than some other methods. However, it is a more straightforward way to round down a large number.Library usage
In this benchmark, the following libraries are used:
Math
library: Used for mathematical functions such as toFixed()
, toPrecision()
, round()
, and trunc()
.~~
: This is not a standard JavaScript operator. It uses the bitwise NOT operator (~
) to round down a number, which is not recommended for production code.toString()
: Used to convert numbers or strings to a string representation.Special JS feature
The benchmark uses the ~~
operator, which is not a standard JavaScript operator. While it works in some cases, it is generally considered bad practice due to potential performance issues and unexpected results.
Other alternatives
If you need to round or truncate numbers in JavaScript, here are some alternative methods:
Math.floor()
or Math.ceil()
for rounding down or up.Number.EPSILON
to compare floating-point numbers for equality.decimal.js
for precise decimal arithmetic.In summary, the benchmark compares the performance of five different methods for rounding or truncating numbers in JavaScript. While each method has its pros and cons, Math.floor fast
is likely the fastest option, while (~~Math.round(*10000)).toString()
may be slower but is often used due to simplicity.