Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Absolute value using Math.abs vs ternary and * -1
(version: 1)
Comparing performance of:
Math.abs vs Ternary
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const val1 = -2000; const val2 = 2000;
Tests:
Math.abs
const result1 = Math.abs(val1); const result2 = Math.abs(val2);
Ternary
const result1 = val1 * (val1 < 0 ? -1 : 1); const result2 = val2 * (val2 < 0 ? -1 : 1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Math.abs
Ternary
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.abs
142133504.0 Ops/sec
Ternary
136922272.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark you provided is designed to compare the performance of two different methods for calculating the absolute value of numbers in JavaScript: using the built-in `Math.abs` function versus a custom implementation using a ternary operator. ### Methods Compared: 1. **Math.abs**: - **Code**: `const result1 = Math.abs(val1); const result2 = Math.abs(val2);` - **Description**: This method leverages the built-in JavaScript function `Math.abs()`, which takes a number as input and returns its absolute value. It is a standard library function provided by JavaScript. - **Pros**: - Simple and easy to read. - Well-optimized and likely efficient, as it is implemented natively in the JavaScript engine. - **Cons**: - Slight overhead due to function call, but this is typically negligible. 2. **Ternary Operator**: - **Code**: `const result1 = val1 * (val1 < 0 ? -1 : 1); const result2 = val2 * (val2 < 0 ? -1 : 1);` - **Description**: This method calculates the absolute value using a ternary conditional operator. The expression checks if the number is less than zero; if true, it multiplies by -1, and otherwise returns the number unchanged. - **Pros**: - No function call overhead; it can be perceived as potentially faster for certain scenarios. - It provides an explicit mathematical manipulation, which some might find instructive in understanding absolute values. - **Cons**: - Slightly less readable for those unfamiliar with this style of coding. - If the code readability is valued, it could be considered more complex than simply using `Math.abs`. ### Benchmark Results: The results indicate how many times each approach could execute per second: - **Ternary** method achieved an execution rate of **162,536,240 ** per second. - **Math.abs** method achieved an execution rate of **156,785,072** per second. From this benchmark, the ternary operator implementation outperformed the `Math.abs` method in terms of executions per second on the given test environment. ### Other Considerations: - **Performance Differences**: While the performance difference might seem significant in this benchmark, it's crucial to note that in real-world applications, performance gains often become negligible, especially for smaller workloads. Readability and maintainability take precedence. The choice of method may depend on the specific use case and team coding standards. - **Optimization by Engines**: Modern JavaScript engines are highly optimized for commonly used functions, including `Math.abs`, and may perform certain optimizations that aren't visible during benchmarking, making the difference in speed less pronounced in actual applications. - **Alternatives**: Other alternatives for calculating absolute values could include using the bitwise OR operator for certain types of values or creating a custom utility function. However, these methods may not necessarily be more efficient or clearer. In summary, this benchmark highlights a performance comparison of two approaches to computing absolute values, demonstrating that while one approach might be faster in this limited test case, considerations like readability and maintainability may lead developers to choose the built-in `Math.abs` in practical scenarios.
Related benchmarks:
asdfasdffdas
If/Else vs Ternary
Math.max/min vs if with const vs ternary operator with const
math floor
fast absolute value (branched & branchless) vs abs vs ** -1
arithm vs ternary
Logical vs Ternary perf (javascript)
ternary vs Math.max vs if / else
1 Math.min() + 1 Math.max() vs 1 Array.reduce()
Comments
Confirm delete:
Do you really want to delete benchmark?