var x = Math.random() * 100000000;
var y;
y = Math.floor(x);
y = ~~x;
y = x >> 0;
y = x | 0;
y = x << 0;
y = x & 0xFFFFFFFFFFFF;
y = x ^ 0x000000000000;
y = Math.trunc(x);
y = x - (x % 1);
y = parseInt(x)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
floor | |
~~ | |
>> 0 | |
| 0 | |
<< 0 | |
& 0xFFFFFFFFFFFF | |
^ 0x000000000000 | |
Math.trunc() | |
x - (x % 1) | |
parseInt() |
Test name | Executions per second |
---|---|
floor | 15543850.0 Ops/sec |
~~ | 15639064.0 Ops/sec |
>> 0 | 15485411.0 Ops/sec |
| 0 | 15633187.0 Ops/sec |
<< 0 | 15578008.0 Ops/sec |
& 0xFFFFFFFFFFFF | 15704023.0 Ops/sec |
^ 0x000000000000 | 15565799.0 Ops/sec |
Math.trunc() | 15451303.0 Ops/sec |
x - (x % 1) | 13428259.0 Ops/sec |
parseInt() | 15511181.0 Ops/sec |
I'll break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to compare the performance of various methods for rounding or truncating floating-point numbers in JavaScript.
Methods Being Compared
floor()
: The built-in Math.floor()
function, which returns the largest integer less than or equal to the input value.~~
(Bitwise Not): A bitwise hack that uses the ~
operator to invert the bits of the input value and then subtracts 1 to get the largest integer less than or equal to the original value.>> 0
: The right shift operator (>>
) with a bit mask of 0, which effectively performs an integer division and returns the largest integer less than or equal to the input value.| 0
(Bitwise OR with 0): A bitwise hack that uses the |
operator to perform a binary operation on the input value and then discards any fractional part by taking the result modulo 1.<< 0
: The left shift operator (<<
) with a bit mask of 0, which effectively performs an integer division and returns the largest integer less than or equal to the input value.& 0xFFFFFFFFFFFF
: A bitwise hack that uses the &
operator with a large bit mask to perform a binary operation on the input value and then discards any fractional part by taking the result modulo this mask.^ 0x000000000000
: A bitwise hack that uses the ^
operator with a small bit mask to perform a binary operation on the input value and then discards any fractional part by taking the result modulo this mask.Math.trunc()
: The built-in Math.trunc()
function, which returns the largest integer less than or equal to the input value using a more optimized algorithm.x - (x % 1)
: A manual implementation that subtracts the fractional part of the input value from the original value.parseInt()
: The built-in parseInt()
function, which attempts to parse a string as an integer.Pros and Cons
Each method has its own trade-offs:
floor()
and Math.trunc()
are straightforward and efficient but may not be suitable for all use cases (e.g., due to potential NaN propagation).~~
and >> 0
are bitwise hacks that can be fast but also fragile and prone to unexpected behavior.| 0
and << 0
are simple but inefficient methods that discard fractional parts without preserving any information.& 0xFFFFFFFFFFFF
and ^ 0x000000000000
are more complex bitwise hacks that may not be suitable for all use cases (e.g., due to potential overflows).Math.trunc()
is optimized for performance but may not be as flexible as other methods.x - (x % 1)
and parseInt()
are manual implementations that can be slower and less efficient than other methods.Benchmark Results
The benchmark results show the execution times for each method, with the fastest time being approximately 0.01-0.1 milliseconds for most of the methods.
Conclusion
This benchmark highlights the importance of choosing the right method for a specific use case when dealing with floating-point numbers in JavaScript. While floor()
and Math.trunc()
are efficient and straightforward, other methods like ~~
and >> 0
may be faster but more fragile. Understanding the trade-offs and potential pitfalls of each method can help developers make informed decisions about which approach to use in their code.