Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.min vs if/else vs ternary operator vs logical or
(version: 0)
Quickest operation to return smaller of 2 numbers
Comparing performance of:
Math.min vs if vs ternary vs logical or
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
this.numberA = Math.random() > .5 || 0; this.numberB = !this.numberA;
Tests:
Math.min
return Math.min(this.numberA, this.numberB);
if
if (this.numberA < this.numberB) return this.numberA; else return this.numberB;
ternary
return (this.numberA < this.numberB) ? this.numberA : this.numberB
logical or
return this.numberA || this.numberB
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Math.min
if
ternary
logical or
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.min
24825534.0 Ops/sec
if
14729632.0 Ops/sec
ternary
14707881.0 Ops/sec
logical or
21442940.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided JSON benchmark definition. **Benchmark Overview** The benchmark measures the performance difference between four different approaches to find the smaller of two numbers: `Math.min()`, `if/else` statement, ternary operator (`? :`), and logical OR operator (`||`). **Comparison of Approaches** 1. **Math.min()**: This function is a built-in JavaScript method that returns the smallest of two or more numbers. It's implemented in native code and optimized for performance. 2. **if/else statement**: This approach uses an if-else statement to compare the two numbers and return the smaller one. It's written in JavaScript and might incur additional overhead due to parsing, execution, and branching. 3. **Ternary operator (`? :`)**: The ternary operator is a shorthand way of writing conditional statements. In this case, it's used as `this.numberA < this.numberB ? this.numberA : this.numberB;`. While concise, the overhead of parsing and executing an expression might be higher compared to a simple if-else statement. 4. **Logical OR operator (`||`)**: This approach uses the logical OR operator to find the smaller number. `this.numberA || this.numberB` will return `this.numberA` if it's not zero, and `this.numberB` otherwise. **Pros and Cons of Each Approach** * **Math.min()**: Pros - highly optimized, fast execution; Cons - relies on a built-in function, might not be implemented in all browsers or platforms. * **if/else statement**: Pros - familiar to most developers, well-documented; Cons - might incur additional overhead due to parsing and branching. * **Ternary operator (`? :`)**: Pros - concise, easy to read; Cons - overhead of parsing and executing an expression. * **Logical OR operator (`||`)**: Pros - simple, efficient; Cons - might not be immediately familiar to all developers. **Library Used** The benchmark uses no external libraries, relying solely on built-in JavaScript features. **Special JS Features or Syntax** None mentioned in the provided information. However, it's worth noting that some browsers have additional features or syntax that might impact performance, such as async/await, generators, or Web Workers. **Alternatives** If you're interested in exploring alternative approaches, consider: * Using a more modern JavaScript method like `Math.min()` with initial values. * Implementing a custom binary search algorithm to find the smaller number. * Comparing the performance of different programming languages (e.g., Node.js vs. browser-based JavaScript). Keep in mind that these alternatives might not be directly applicable or comparable to this specific benchmark. In conclusion, this benchmark provides an interesting comparison of four simple approaches to finding the smaller of two numbers, highlighting the trade-offs between built-in functions, if-else statements, ternary operators, and logical OR operators.
Related benchmarks:
Math.min vs if/else vs ternary operator for Int
Math.max/min vs if vs ternary operatorsd
Math.min vs if/else vs ternary operator test 2
Math.min vs if/else vs ternary operator test 3
Comments
Confirm delete:
Do you really want to delete benchmark?