Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Abs vs ternary to find difference between two number
(version: 0)
Benchmark.js
Comparing performance of:
abs vs ternary
Created:
3 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var one = 5 var two = 2
Tests:
abs
var a = Math.abs(two - one)
ternary
var a = two > one ? two - one : one - two
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
abs
ternary
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0
Browser/OS:
Firefox 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
abs
902.7M/s
ternary
840.3M/s
View exact numbers
Test name
Executions per second
✓
abs
902,704,576 Ops/sec
ternary
840,340,672 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark on MeasureThat.net compares two ways to calculate the difference between two numbers: using the `Math.abs()` function and using a ternary operator. **Options Compared:** * **`Math.abs(two - one)`:** This approach first calculates the difference between `two` and `one`, then uses the `Math.abs()` function to ensure a positive result regardless of the order of the numbers. * **`ternary ? two - one : one - two`:** This approach uses a ternary operator, a concise way to write an if-else statement in JavaScript. It checks if `two` is greater than `one`. If true, it subtracts `one` from `two`; otherwise, it subtracts `two` from `one`. **Pros and Cons:** * **`Math.abs()`:** * **Pros:** More explicit, clearly shows the intention of finding the absolute difference. * **Cons:** Potentially slightly less performant due to the extra function call. * **Ternary Operator:** * **Pros:** More concise and potentially slightly faster due to fewer operations. * **Cons:** Can be less readable for beginners, especially if the logic is more complex. **Other Considerations:** * **Libraries:** This benchmark doesn't use any external libraries. * **JavaScript Features:** It utilizes the basic `Math` object and the ternary operator, both common features in JavaScript. **Alternatives:** While not directly compared here, other methods exist to calculate the difference: * **Using a third variable:** You could assign the absolute value of `two - one` or vice versa to a third variable, avoiding either function call or conditional statements. * **Bitwise XOR (`^`) operator:** For specific cases where you only need the absolute difference between two numbers and performance is critical, bitwise XOR can offer even greater efficiency. Let me know if you have any other questions!
Related benchmarks
floor vs trunc vs bit shift
Math.max/min vs if vs ternary operatorsd
ParseInt vs conditional ~~
Comments
Confirm delete:
Do you really want to delete benchmark?