Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.min vs if/else vs ternary operator test 2
(version: 0)
Quickest operation to return smaller of 2 numbers
Comparing performance of:
Math.min vs if vs ternary
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
this.numberA = Math.random() * 1000; this.numberB = Math.random() * 1000; this.numberC = Math.random() * 1000;
Tests:
Math.min
return Math.min(this.numberA, this.numberB, this.numberC);
if
if (this.numberA < this.numberB) { return this.numberA; }else{ if (this.numberB < this.numberC) { return this.numberB }else{ return this.numberC; } }
ternary
return (this.numberA < this.numberB) ? this.numberA : ( this.numberB < this.numberC?this.numberB:this.numberC)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math.min
if
ternary
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.min
10539541.0 Ops/sec
if
1396300672.0 Ops/sec
ternary
1398929152.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The test compares three different ways to return the smaller of two numbers: `Math.min()`, an `if-else` statement, and a ternary operator. The script generates three random numbers (`numberA`, `numberB`, and `numberC`) using JavaScript code provided in the "Script Preparation Code" section. **Test Case Options** 1. **Math.min()**: Uses the built-in `Math.min()` function to find the smallest of the three numbers. * Pros: Fast, widely supported, and easy to use. * Cons: May not be suitable for all edge cases or specific requirements (e.g., performance-critical code). 2. **if-else statement**: Manually checks each number against others using an `if` statement. * Pros: Customizable, can handle specific requirements or edge cases. * Cons: Generally slower and more complex than the other options. 3. **Ternary operator**: Uses a single expression with multiple conditions to return the smallest of the three numbers. * Pros: Concise, fast, and easy to read. * Cons: May not be as readable or maintainable for complex logic. **Library Used** In this benchmark, no specific library is used beyond JavaScript's built-in `Math` functions. However, some browsers (like Chrome) may include additional libraries or extensions that could potentially affect performance. **Special JS Features or Syntax** None of the test cases explicitly use special JavaScript features like ES6 modules, async/await, or Promises. The focus is on comparing basic operation methods. **Benchmark Result Interpretation** The latest benchmark result shows: * `Math.min()` has an average execution speed of approximately 1054 executions per second. * The `if`-else statement averages around 13,649 executions per second. * The ternary operator averages about 13989291.5 executions per second (which is significantly faster than the other two options). **Other Alternatives** In theory, you could use other methods to find the smallest of two numbers, such as: * Using a recursive function * Utilizing bitwise operations (e.g., comparing two values using `&` and `|`) * Leveraging bitwise shifts or mask-based comparisons However, these alternatives are likely to be slower, more complex, and less readable than the options tested in this benchmark. In summary, this benchmark compares three fundamental approaches to finding the smallest of two numbers: a built-in function (`Math.min()`), an `if-else` statement, and a concise ternary operator. The results highlight the significant performance difference between these methods.
Related benchmarks:
Math.min vs if/else vs ternary operator
Math.min vs if/else vs ternary operator for Int
Math.min vs if/else vs ternary operator vs logical or
Math.min vs if/else vs ternary operator test 3
Comments
Confirm delete:
Do you really want to delete benchmark?