Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math floor vs bitwise operation
(version: 2)
Math floor vs bitwise operation
Comparing performance of:
Math.floor vs ~~
Created:
one year ago
by:
Registered User
Jump to the latest result
Tests:
Math.floor
Math.floor(13.37) Math.floor(13.67)
~~
~~13.37 ~~13.67
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Math.floor
~~
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.floor
114536048.0 Ops/sec
~~
141894448.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark described in the provided JSON measures the performance of two different methods for flooring a number in JavaScript: using the `Math.floor()` function and using a bitwise NOT operator (`~~`). ### Test Cases Overview 1. **Math.floor()** - **Benchmark Definition:** `Math.floor(13.37)` & `Math.floor(13.67)` - **Test Name:** `Math.floor` - **Description:** This method takes a numeric value as input and returns the largest integer less than or equal to that number. In this test, it operates on two floating-point numbers. 2. **Bitwise NOT Operator (`~~`)** - **Benchmark Definition:** `~~13.37` & `~~13.67` - **Test Name:** `~~` - **Description:** The use of `~~` is a shorthand technique in JavaScript that effectively floors a number by converting it into a 32-bit integer. It performs a bitwise NOT operation twice, which leads to a conversion of the floating-point number to an integer. ### Performance Results The benchmark results show the executed operations per second for each method: - **Bitwise NOT (`~~`)**: 141,894,448 executors per second. - **Math.floor()**: 114,536,048 executors per second. ### Comparison of Options #### Pros and Cons 1. **Math.floor()** - **Pros:** - Readable and self-explanatory; conveys intent clearly to anyone reading the code. - Complies with the standard specification for floating-point number handling. - **Cons:** - Slower performance compared to the bitwise operation due to function call overhead and possible internal floating-point checks. 2. **Bitwise NOT (`~~`)** - **Pros:** - Can be significantly faster than `Math.floor()` in terms of execution speed due to lower-level bit manipulation, appealing in performance-critical sections of code. - It works well for positive numbers, and it’s compact, making it a popular choice in performance benchmarks. - **Cons:** - Limited to converting numbers within the range of 32-bit signed integers; it's not suitable for all potential numeric inputs. - Less readable for those unfamiliar with the trick, which can reduce maintainability. ### Other Considerations and Alternatives While the benchmark compares just these two methods, there are alternative approaches to flooring a number in JavaScript: 1. **Using Math.trunc()** - This method trims off the decimal part without rounding, effectively behaving similarly to `Math.floor()` for positive numbers. - Performance-wise, it is often close to `Math.floor()` but is also a method call which might incur similar overhead. 2. **Using Bitwise OR with Zero (`| 0`)** - This is another bitwise trick to convert a number to an integer. Similar to `~~`, it can perform flooring but has the same limitations regarding range. In summary, the benchmark clearly shows a performance advantage for the bitwise operation, but with increased performance comes reduced readability and potential issues with input values. The choice between these methods may often depend on the specific application requirements, balancing the need for code clarity against execution efficiency.
Related benchmarks:
Bitwise floor
Bitwise floor
TuggaKoden
int int int
Math floor vs | vs shift
Math.floor
Bitwise, Bitwise not, and Math floor
floor vs bitwise
Flooring with different Bitwise operators Fixed
Testing bitwise
Comments
Confirm delete:
Do you really want to delete benchmark?