Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Truncation Methods
(version: 0)
Testing the various methods for truncating a number to an integer
Comparing performance of:
Bitwise complement vs Bitwise shift vs Math.trunc() vs Math.round()
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array.from({length: 10000}, _ => Math.random() * 1000);
Tests:
Bitwise complement
a.map(i => ~~i);
Bitwise shift
a.map(i => i >>> 0);
Math.trunc()
a.map(i => Math.trunc(i));
Math.round()
a.map(i => Math.round(i));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Bitwise complement
Bitwise shift
Math.trunc()
Math.round()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:140.0) Gecko/20100101 Firefox/140.0
Browser/OS:
Firefox 140 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Bitwise complement
23610.0 Ops/sec
Bitwise shift
22285.3 Ops/sec
Math.trunc()
13495.5 Ops/sec
Math.round()
13479.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and benchmark preparation code. **Benchmark Definition** The benchmark is designed to test various methods for truncating a number to an integer. This is achieved by creating an array of 10,000 random numbers between 0 and 1000, and then applying different methods to truncate these numbers. **Script Preparation Code** The script preparation code creates an array `a` with 10,000 elements, each containing a random value between 0 and 1000. The `_` variable is likely a shorthand for the arrow function parameter. ```javascript var a = Array.from({length: 10000}, _ => Math.random() * 1000); ``` **Individual Test Cases** There are four test cases, each with its own benchmark definition: 1. `a.map(i => ~~i);`: This uses the bitwise complement operator (`~~`) to truncate the number. 2. `a.map(i => i >>> 0);`: This uses the bitwise shift operator (`>>>`) to truncate the number. 3. `a.map(i => Math.trunc(i));`: This uses the `Math.trunc()` function to truncate the number. 4. `a.map(i => Math.round(i));`: This uses the `Math.round()` function to truncate the number. Let's briefly describe each approach: * **Bitwise complement (`~~i`)**: This method is not as straightforward as it seems. The bitwise complement operator flips all the bits of a number, which can lead to incorrect results for negative numbers. For example, `-5` becomes `1` when using this method. * **Bitwise shift (`i >>> 0`)**: This method works by shifting the bits of the number to the right and then using the least significant bit as the truncated value. This approach is more straightforward but can be slower than other methods for large numbers. * **Math.trunc()**: This function returns the largest possible integer less than or equal to the input value. It is a standard JavaScript function that uses a combination of bit manipulation and rounding algorithms to produce accurate results. * **Math.round()**: This function returns the closest integer to the input value, which can lead to incorrect results for non-integer values. In this benchmark, it's used to test the truncation behavior. **Pros and Cons** * **Bitwise complement (`~~i`)**: Pros: simple to implement, cons: can produce incorrect results for negative numbers. * **Bitwise shift (`i >>> 0`)**: Pros: straightforward, cons: slower for large numbers. * **Math.trunc()**: Pros: accurate, consistent, and well-tested. Cons: might be slower than other methods due to its internal algorithm. * **Math.round()**: Pros: simple to implement, cons: can produce incorrect results for non-integer values. **Library** None of the individual test cases rely on external libraries. **Special JS feature or syntax** The bitwise shift operator (`>>>`) is a special JavaScript operator that shifts the bits of a number to the right and then uses the least significant bit as the truncated value. This approach is specific to JavaScript and is not commonly used in other programming languages. **Alternatives** Other methods for truncating numbers include: * `Math.floor()`: Returns the largest possible integer less than or equal to the input value. * `Number.EPSILON`: A small value that can be added to a number before taking its floor value, effectively "rounding down". * Custom implementations using bit manipulation and arithmetic operations. Note that these alternatives might not be included in the benchmark as they are not as straightforward or well-tested as the methods used in this benchmark.
Related benchmarks:
toFixed vs toPrecision vs Math.round() vs Math.floorfast vs new Math.trunc vs numeraljs
toFixed vs toPrecision vs Math.floor() vs Math.floorfast vs new Math.trunc
Removing Decimal Math.Trunc vs toFixed
toFixed vs toPrecision vs Math.round() with constant multiplier
toFixed vs Math.round() sd6f54sd6f54s6df54ds6f
Comments
Confirm delete:
Do you really want to delete benchmark?