Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Integer division test
(version: 1)
Comparing performance of:
floor vs trunc vs bitwiseOr vs doubleNot
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
function floor(a,b) { return Math.floor(a/b); } function trunc(a,b) { return Math.trunc(a/b); } function bitwiseOr(a,b){ return (a/b) | 0; } function doubleNot(a,b){ return ~~(a/b); }
Tests:
floor
floor(10,3)
trunc
trunc(10,3)
bitwiseOr
bitwiseOr(10,3)
doubleNot
doubleNot(10,3)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
floor
trunc
bitwiseOr
doubleNot
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:133.0) Gecko/20100101 Firefox/133.0
Browser/OS:
Firefox 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
floor
293627904.0 Ops/sec
trunc
397522752.0 Ops/sec
bitwiseOr
939546688.0 Ops/sec
doubleNot
893266496.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
In the provided benchmark definition, four different approaches to performing integer division in JavaScript are being tested: `Math.floor`, `Math.trunc`, bitwise OR, and double NOT. The benchmark measures how quickly each approach can execute the division operation of `10` by `3`. ### Test Options 1. **floor(a, b)**: - **Function**: Uses `Math.floor` to return the largest integer less than or equal to the division result. - **Pros**: - Accurate for all scenarios, including negative inputs. - **Cons**: - Slightly slower compared to other methods due to function overhead and more complex calculations. 2. **trunc(a, b)**: - **Function**: Utilizes `Math.trunc` to return the integer part of the division, effectively discarding the decimal part. - **Pros**: - Fast and straightforward for positive integers. - **Cons**: - Performance can differ with negative inputs, as it truncates towards zero. 3. **bitwiseOr(a, b)**: - **Function**: Applies the bitwise OR operator `|` with `0` to achieve integer division. - **Pros**: - Fastest among the tested methods because it operates at the binary level and effectively discards the decimal. - **Cons**: - May not handle negative values as expected (rounding towards zero instead of towards negative infinity). 4. **doubleNot(a, b)**: - **Function**: Uses the double NOT operator `~~` to convert the division result into an integer. - **Pros**: - Very fast and concise similar to the bitwise OR approach. - **Cons**: - Also behaves unfavorably with negative numbers, truncating towards zero. ### Performance Results From the benchmark results: - **`bitwiseOr`** achieved the highest rate with approximately **939 million executions per second**, indicating that this method is highly efficient. - **`doubleNot`** was close behind with roughly **893 million executions per second**. - **`trunc`** followed, with around **397 million executions per second**. - **`floor`** was the slowest at about **294 million executions per second**. ### Considerations - **Performance vs. Accuracy**: The bitwise and double NOT methods are significantly faster but may not give the expected results for negative numbers. If the context strongly dictates accuracy with negative values, `Math.floor` or `Math.trunc` would be preferable despite their lower speed. - **Real-World Use Cases**: When deciding which method to use, consider the nature of your data (positive vs. negative) and if speed is critical in performance-sensitive code sections. ### Alternatives In addition to the methods tested: - **Using Regular Expressions or String Manipulation**: Though less common for integer division, some alternatives involve converting results to strings and manipulating them; however, this comes with performance drawbacks. - **Native Integer Support in WebAssembly**: For more performance-intensive applications, considering WebAssembly (Wasm) may be beneficial. This allows for more complex mathematical operations with potentially increased efficiency, especially for large computations. In summary, the choice of method for integer division in JavaScript should balance performance requirements with the need for correctness based on the expected input values.
Related benchmarks:
Anonymous Function in Loop
Anonymous Function in Loop
Anonymous Function in Loop
sadddsasad
fdsdfgsdfg
Numver.isInteger vs raw
Math.min vs. ternary
Edge wrapping - ternary vs magic constant
Anonymous Function in Loop 2
Comments
Confirm delete:
Do you really want to delete benchmark?