Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
branchless max vs Math.max (cached & uncached)
(version: 0)
Comparing performance of:
cached Math.max vs fastMax vs Math.max
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var fastMax = (a, b) => (a - (((a - b) >> 31) & (a - b))); var max = Math.max;
Tests:
cached Math.max
max(Math.random(), Math.random(), Math.random())
fastMax
fastMax(Math.random(), Math.random(), Math.random())
Math.max
Math.max(Math.random(), Math.random(), Math.random())
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
cached Math.max
fastMax
Math.max
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.6 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
cached Math.max
4333355.0 Ops/sec
fastMax
4473287.5 Ops/sec
Math.max
4039006.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **What is being tested?** The benchmark measures the performance difference between three approaches to find the maximum value of three random numbers: 1. `fastMax`: A custom implementation of `Math.max` that uses bitwise operations to avoid calling the `Math.max` function directly. 2. `cached Math.max`: An implementation of `Math.max` where the result is cached (i.e., stored in a variable) and reused instead of recalculating it every time. 3. `Math.max`: The built-in `Math.max` function from JavaScript's standard library. **Options compared** The benchmark compares the performance of these three approaches: * **Pros and Cons:** + `fastMax`: This approach avoids calling `Math.max`, which can be expensive. However, it uses bitwise operations, which might not be as readable or maintainable as other approaches. + `cached Math.max`: This approach reuses the cached result, which can reduce overhead compared to recalculating the maximum value every time. However, it might consume more memory if the benchmark is run multiple times. + `Math.max`: This is the built-in implementation that provides good performance and readability. However, it's not optimized for this specific use case. * **Other considerations:** + The benchmark doesn't consider other factors like input distribution or edge cases, which might affect the results. **Library usage** None of the test cases uses any external libraries beyond JavaScript's standard library (`Math`). **Special JS features or syntax** The custom implementation `fastMax` uses bitwise operations to avoid calling `Math.max`. Specifically, it uses the following trick: ```javascript var fastMax = (a, b) => (a - (((a - b) >> 31) & (a - b))); ``` This expression takes advantage of the fact that in two's complement representation, the most significant bit of an integer represents the sign. By subtracting `b` from `a`, shifting the result to the right by 31 bits (`>>> 31`), and then performing a bitwise AND with `a - b`, the resulting value is equivalent to the maximum of `a` and `b`. This trick avoids calling `Math.max`, making it faster. Overall, the benchmark provides a simple and straightforward way to compare the performance of different approaches to finding the maximum value of three random numbers.
Related benchmarks:
Math.max/min vs if vs ternary vs bitwise & ~~ - 4 numbers
Math.max/min vs if vs ternary vs bitwise & ~~ & lodash - 5 numbers
array math.max (3 variants) vs for loop (4 variants)
array math.max (3 variants) vs for loop (5 variants)
Comments
Confirm delete:
Do you really want to delete benchmark?