Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
if vs ternary vs multiplication
(version: 0)
Comparing performance of:
if vs ternary vs multiply vs multiply const
Created:
4 years ago
by:
Guest
Go to the latest result
Script Preparation code:
this.n = Math.floor(Math.random() * 10000);
Tests:
if
if(this.n%2) { return 3*this.n+1; } else { return this.n/2; }
ternary
return (this.n%2)?3*this.n+1:this.n/2;
multiply
return (this.n%2)*(3*this.n+1)+(!(this.n%2))*this.n/2;
multiply const
const n2=this.n%2; return n2*(3*this.n+1)+!n2*this.n/2;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
if
ternary
multiply
multiply const
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Chrome OS 14541.0.0
View result in a separate tab
Embed
Embed Benchmark Result
if
4.9M/s
ternary
4.8M/s
multiply const
4.5M/s
multiply
2.5M/s
View exact numbers
Test name
Executions per second
✓
if
4,880,684 Ops/sec
ternary
4,838,282 Ops/sec
multiply const
4,467,896 Ops/sec
multiply
2,511,325 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **What is tested?** The provided JSON represents a benchmark that tests three different approaches to evaluate an expression: `if`, `ternary` (also known as conditional operator), and `multiplication`. The expressions are: 1. `if(this.n%2){ return 3*this.n+1; } else { return this.n/2; }` 2. `return (this.n%2)?3*this.n+1:this.n/2;` (ternary operator) 3. `(this.n%2)*(3*this.n+1)+(!(this.n%2))*this.n/2;` (multiplication approach with a separate variable) 4. `const n2=this.n%2;return n2*(3*this.n+1)+!n2*this.n/2;` (another multiplication approach with a separate constant variable) **Options compared** The benchmark compares the performance of three approaches to evaluate an expression: `if`, `ternary`, and two multiplication approaches. The options are: * **If**: uses an explicit `if-else` statement * **Ternary**: uses a conditional operator (`?`) with two possible values * **Multiplication (const)**: uses a separate variable to store the condition result, then multiplies the value using that variable * **Multiplication (without const)**: uses the bitwise NOT operator (!) to invert the condition result, and then multiplies the value **Pros and cons of each approach** 1. **If**: Pros - easy to read and understand, can be used in many situations. Cons - may incur a performance penalty due to the branching. 2. **Ternary**: Pros - concise and efficient, eliminates the need for an `if-else` statement. Cons - less readable than `if-else`, and some older JavaScript engines might not support it. 3. **Multiplication (const)**: Pros - can be more efficient than the other approaches since it avoids branching, but may require additional memory to store the condition variable. Cons - requires an extra variable, which can increase memory usage. 4. **Multiplication (without const)**: Pros - similar to `multiplication (const)`, but without the need for an extra variable. Cons - still requires the bitwise NOT operator, which might be slower than other approaches. **Other considerations** * The use of bitwise operators like `!` can lead to performance issues due to their complexity. * Modern JavaScript engines often have optimizations that make some approaches faster or more efficient than others. **Library used (if any)** None, this benchmark does not rely on a specific library. **Special JS feature or syntax** The ternary operator (`?`) is a special feature in JavaScript that allows for concise conditional expressions. The bitwise NOT operator (`!`) can be used to invert a boolean value. Both are widely supported in modern JavaScript engines. Now, let's talk about the benchmark results... **Benchmark results analysis** The provided benchmark results show the executions per second (EPS) for each test case on a Chrome Mobile 116 device running Android 10. The results suggest that: * The `ternary` approach is the fastest, followed closely by the `multiplication (const)` and then the `if` approaches. * The `multiplication (without const)` approach is slower than all other options. Please note that these results are specific to this particular test case and may not generalize across different scenarios or environments.
Related benchmarks
Math.floor vs bitwise <<
Math.min vs if/else vs ternary operator for Int
Logical vs Ternary perf (javascript)
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?