Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
switch(true) vs if-else
(version: 0)
Comparing performance of:
switch(true) vs if-else
Created:
2 years ago
by:
Guest
Go to the latest result
Tests:
switch(true)
const a = 1; switch (true) { case a === 5: console.log('a is 5'); break; case a === 4: console.log('a is 4'); break; case a === 3: console.log('a is 4'); break; case a === 41: console.log('a is 4'); break; case a === 31: console.log('a is 4'); break; case a === 42: console.log('a is 4'); break; case a === 33: console.log('a is 4'); break; case a === 44: console.log('a is 4'); break; case a === 35: console.log('a is 4'); break; case a === 46: console.log('a is 4'); break; case a === 37: console.log('a is 4'); break; case a === 48: console.log('a is 4'); break; case a === 39: console.log('a is 4'); break; case a === 40: console.log('a is 4'); break; case a === 311: console.log('a is 4'); break; case a === 422: console.log('a is 4'); break; case a === 333: console.log('a is 4'); break; case a === 444: console.log('a is 4'); break; case a === 355: console.log('a is 4'); break; case a === 2: console.log('a is 2'); break; case a === 1: console.log('a is 1'); break; }
if-else
const a = 1; if (a == 5) { console.log('a is 5'); } else if (a == 4) { console.log('a is 4'); } else if (a == 3) { console.log('a is 4'); } else if (a == 3) { console.log('a is 4'); } else if (a == 3) { console.log('a is 4'); } else if (a == 3) { console.log('a is 4'); } else if (a == 3) { console.log('a is 4'); } else if (a == 3) { console.log('a is 4'); } else if (a == 3) { console.log('a is 4'); } else if (a == 3) { console.log('a is 4'); } else if (a == 3) { console.log('a is 4'); } else if (a == 3) { console.log('a is 4'); } else if (a == 3) { console.log('a is 4'); } else if (a == 3) { console.log('a is 4'); } else if (a == 3) { console.log('a is 4'); } else if (a == 2) { console.log('a is 2'); } else if (a == 1) { console.log('a is 1'); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
switch(true)
if-else
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 YaBrowser/25.2.0.0 Safari/537.36
Browser/OS:
Yandex Browser 25 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
if-else
417K/s
switch(true)
398K/s
View exact numbers
Test name
Executions per second
✓
if-else
416,841 Ops/sec
switch(true)
397,569 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and provide an analysis of the results. **Benchmark Overview** The benchmark measures the performance difference between using a `switch` statement with a conditional expression (`true`) versus an `if-else` chain in JavaScript. The test cases are identical, except for the syntax used. **Switch Statement (with true)** In this approach, the `switch` statement is used to evaluate the condition `a === 5`. Since `true` is always evaluated as true, the entire `switch` block will be executed for each case that matches. However, in this specific test case, none of the cases match, so the `switch` block will still be executed but with no branching. **If-Else Chain** In this approach, an `if-else` chain is used to evaluate the condition `a == 5`. The first matching case will be executed if true. However, in this test case, there are multiple consecutive `if-else` statements that all match when `a == 3`, which is a different value than what we want to check. **Pros and Cons** * **Switch Statement (with true)**: + Pros: Can potentially execute faster due to the conditional expression being evaluated once. + Cons: May execute more code than necessary since none of the cases match. * **If-Else Chain**: + Pros: Executes only when a condition is met, which can be beneficial for performance in some scenarios. + Cons: Can lead to slower execution due to the multiple consecutive checks. **Library and Special JS Features** There are no libraries mentioned in the benchmark definition. However, it's worth noting that modern JavaScript engines often have optimizations and heuristics to improve performance in such cases. **Test Results** The latest benchmark results show: * **Switch (with true)**: Executions per second = 384345.5625 * **If-Else**: Executions per second = 376402.40625 The results suggest that the `switch` statement with a conditional expression (`true`) is slightly faster than the `if-else` chain in this specific test case. **Other Alternatives** Other alternatives for evaluating simple conditions in JavaScript include: * Using the ternary operator (`?:`) * Using `&&` and `||` operators * Using `===` or `==` comparisons with a conditional expression However, these alternatives may not provide the same level of readability and maintainability as the `if-else` chain used in this benchmark. In conclusion, the results suggest that the `switch` statement with a conditional expression (`true`) is slightly faster than the `if-else` chain in this specific test case. However, the performance difference may not be significant enough to justify using this approach in most scenarios.
Related benchmarks
Switch vs If else CNC
JS switch vs if/else if
map vs ifelse vs switch test
Comments
Confirm delete:
Do you really want to delete benchmark?