Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
simple Math.max vs ternary
(version: 0)
Comparing performance of:
ternary vs Math.max
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Math.round(Math.random()*1000),b = Math.round(Math.random()*1000),c;
Tests:
ternary
c = a>b?a:b;
Math.max
c = Math.max(a,b);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
ternary
Math.max
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
ternary
1319313152.0 Ops/sec
Math.max
1990186624.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of two approaches: using a ternary operator (`c = a>b?a:b;`) versus using the `Math.max()` function (`c = Math.max(a,b);`). **Options Compared** * **Ternary Operator**: This approach uses a single expression to evaluate and assign a value to `c`. The syntax is `x ? y : z`, where `x` is evaluated as true or false, and `y` is executed if `x` is true, while `z` is executed if `x` is false. * **Math.max()**: This approach uses the built-in `Math.max()` function to find the maximum value between `a` and `b`. **Pros and Cons** * **Ternary Operator** + Pros: - Simple and concise syntax - Can be more readable for simple conditions - No additional library dependencies required + Cons: - May not be as efficient due to the need to evaluate the condition - Can lead to longer execution times if the condition is complex or frequent * **Math.max()** + Pros: - More efficient, as it's implemented in native code and optimized for performance - Less prone to errors due to being a well-defined function - Supports more advanced comparison scenarios (e.g., finding the maximum of multiple values) + Cons: - Requires importing the `Math` object or accessing its properties (`Math.max()`) - May have overhead due to function call and parameter passing **Library: Math** The `Math` library provides a set of mathematical functions, including `max()`. The `Math.max()` function takes one or more arguments and returns the largest value among them. **Special JS Feature/Syntax: None mentioned in this benchmark.** **Benchmark Preparation Code** The script preparation code is: ```javascript var a = Math.round(Math.random()*1000), b = Math.round(Math.random()*1000); ``` This code generates two random integers `a` and `b`, which are used as input for the comparisons. **Individual Test Cases** Each test case consists of a unique benchmark definition, which is executed to measure performance. In this case, there are only two test cases: 1. **Ternary**: Evaluates the ternary operator expression (`c = a>b?a:b;`) 2. **Math.max**: Evaluates the `Math.max()` function expression (`c = Math.max(a,b);`) **Latest Benchmark Result** The latest benchmark result shows that Chrome 129 on Windows Desktop achieved: * Ternary: 2857204.25 executions per second * Math.max: 2467943.75 executions per second This suggests that the `Math.max()` approach is faster than the ternary operator approach in this specific test case. **Other Alternatives** For similar benchmarks, you might consider using other approaches or variations, such as: * Using other comparison operators (e.g., `>=`, `<=`, etc.) * Adding more complex logic to the comparisons * Experimenting with different data distributions or input sizes * Comparing performance on different browsers, devices, or platforms Keep in mind that benchmark results can be highly dependent on specific use cases, hardware configurations, and optimization techniques.
Related benchmarks:
toFixed vs toPrecision vs Math.round() vs Math.floorfast vs MDN round_to_precision
toFixed vs Math.round()
toFixed vs toPrecision vs Math.round() to 1 decimal place
toFixed vs toPrecision vs Math.round() 2 decimal
toFixed vs Math.round() with numbers222
Comments
Confirm delete:
Do you really want to delete benchmark?