var original = 3.14;
var truncated1 = Math.trunc(original)
const truncated1 = ~~original;
const truncated2 = original & -1;
const truncated3 = original | 0;
const truncated4 = original ^ 0;
const truncated5 = original >> 0;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Math.trunc() | |
Double negation | |
Bitwise AND with -1 | |
Bitwise OR with 0 | |
Bitwise XOR with 0 | |
Bitwise shifting by 0 |
Test name | Executions per second |
---|---|
Math.trunc() | 438931.0 Ops/sec |
Double negation | 1121380.8 Ops/sec |
Bitwise AND with -1 | 1287382.6 Ops/sec |
Bitwise OR with 0 | 1347989.9 Ops/sec |
Bitwise XOR with 0 | 1277862.6 Ops/sec |
Bitwise shifting by 0 | 1315309.4 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested on MeasureThat.net.
Benchmark Definition
The benchmark definition describes six different techniques to truncate float-point numbers in JavaScript:
Math.trunc(original)
~~original
)original & -1
)original | 0
)original ^ 0
)original >> 0
)Each technique is represented as a separate benchmark definition, which includes the JavaScript code to perform the truncation and the name of the test case.
Pros and Cons of Each Approach
trunc()
function, which is likely implemented in native code. It's fast and efficient but may not be as readable or intuitive for developers.~~original
): This method uses a clever trick by applying two consecutive negations to truncate the number. While it's concise, it may lead to confusion among developers who are not familiar with this idiom.original & -1
): This method uses bitwise operations to clear the fractional part of the number. It's a common technique in C-style programming but might be less familiar to JavaScript developers.original | 0
): Similar to the previous approach, this method uses bitwise operations to clear the fractional part of the number. However, it may not work as expected for negative numbers due to sign bit masking.original ^ 0
): This method is essentially equivalent to the previous one but might be slightly more efficient due to its simplicity.original >> 0
): This method simply shifts the number right by zero, which has no effect on the value. While it's a valid operation, it's unlikely to provide any benefits over other methods.Library Usage
None of the benchmark definitions explicitly uses a JavaScript library. However, if we look at the Math.trunc()
implementation, it's likely that the browser's JavaScript engine is using a native implementation of this function, which might be optimized for performance.
Special JS Features or Syntax
There are no special JavaScript features or syntax used in these benchmark definitions, aside from the double negation trick (~~original
), which is an idiosyncratic feature of JavaScript. However, it's worth noting that some browsers may optimize certain operations more than others due to their specific implementation details.
Alternatives
Other alternatives for truncating float-point numbers in JavaScript include:
decimal.js
or Big.js
, which provide decimal arithmetic and rounding functions.In conclusion, the MeasureThat.net benchmark provides an interesting comparison of different techniques for truncating float-point numbers in JavaScript. While each method has its pros and cons, Math.trunc(original)
is likely the fastest and most efficient approach due to its native implementation.