Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.min vs. ternary
(version: 0)
Comparing performance of:
Math.min vs Ternary vs Bitwise min
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Math.random() * 1000; var b = Math.random() * 1000; function ternary_min(x, y) { return x < y ? x : y; } function bitwise_min(x, y) { return y ^ ((x ^ y) & -(x < y)); }
Tests:
Math.min
var min = Math.min(a, b);
Ternary
var min = ternary_min(a, b);
Bitwise min
var min = bitwise_min(a, b);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math.min
Ternary
Bitwise min
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/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.min
149503248.0 Ops/sec
Ternary
146269888.0 Ops/sec
Bitwise min
150362432.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is being tested?** The provided JSON represents a JavaScript microbenchmark that compares three approaches to find the minimum value between two numbers: using the built-in `Math.min()` function, a ternary operator-based implementation, and a bitwise operation-based implementation. **Options compared:** 1. **Math.min()**: The built-in `Math.min()` function in JavaScript. 2. **Ternary min**: A custom implementation using a ternary operator to compare and return the minimum value. 3. **Bitwise min**: A custom implementation using bitwise operations to compare and return the minimum value. **Pros and cons of each approach:** 1. **Math.min()**: * Pros: Fast, efficient, and widely supported across browsers and platforms. * Cons: May have some performance overhead due to function call and lookup. 2. **Ternary min**: * Pros: Simple, easy to understand, and can be optimized for specific use cases. * Cons: May be slower than `Math.min()` or bitwise operations due to the complexity of the ternary operator logic. 3. **Bitwise min**: * Pros: Can be highly optimized and competitive with `Math.min()` in terms of performance. * Cons: Requires a good understanding of bitwise operations and their limitations, which can make it more complex to implement and maintain. **Library usage:** None of the provided test cases use any external libraries. The benchmark only relies on built-in JavaScript functions and operators. **Special JS features or syntax:** The `Bitwise min` implementation uses a bitwise operation to compare the values, specifically `y ^ ((x ^ y) & -(x < y))`. This is an optimization technique that takes advantage of the properties of bitwise XOR and AND operations to determine which value is smaller without explicitly comparing them. **Other alternatives:** There may be other approaches to find the minimum value between two numbers in JavaScript, such as using a binary search algorithm or a more complex mathematical formula. However, `Math.min()`, the ternary min, and bitwise min are some of the most common and well-known methods used in JavaScript. In summary, the benchmark compares three popular methods for finding the minimum value between two numbers: built-in `Math.min()`, a custom implementation using a ternary operator (`ternary_min`), and another custom implementation using bitwise operations (`bitwise_min`). The pros and cons of each approach highlight their respective strengths and weaknesses in terms of performance, complexity, and usability.
Related benchmarks:
Math.min vs. ternary vs ternaryv2
Math.max/min vs if vs ternary vs bitwise & ~~ & lodash - 5 numbers
Math.max/min vs function ternary vs inline ternary
Clamping via min-max or ternary operator
Comments
Confirm delete:
Do you really want to delete benchmark?