Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
switch vs if
(version: 0)
Comparing performance of:
switch vs if
Created:
8 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div class="test"></div>
Script Preparation code:
var v = 8;
Tests:
switch
switch (true) { case v === 1: break; case v === 2: break; case v === 3: break; case v === 4: break; case v === 5: break; case v === 6: break; case v === 7: break; default: ; }
if
if (v === 1) { } else if (v === 2) { } else if (v === 3) { } else if (v === 4) { } else if (v === 5) { } else if (v === 6) { } else if (v === 7) { } else if (v === 8) { } else { }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
switch
if
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:143.0) Gecko/20100101 Firefox/143.0
Browser/OS:
Firefox 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
switch
42150480.0 Ops/sec
if
37148368.0 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark compares the performance of using a `switch` statement versus a chain of `if...else if` statements in JavaScript to check against various values. **Options Compared:** * **`switch` Statement:** This evaluates an expression and executes the code block associated with the matching case. In this example, each `case` checks if the variable `v` equals a specific number (1 through 8). If no match is found, it executes the `default` block. * **Chain of `if...else if` Statements:** This sequentially checks conditions. Each `if` statement evaluates to true or false, and if true, its code block is executed. If none are true, the final `else` block executes. Here, each `if` statement checks if `v` equals a specific number (1 through 8). **Pros and Cons:** * **`switch` Statement:** * **Pros:** Generally faster for many cases because it uses a jump table optimization under the hood. Can be more concise when dealing with multiple comparisons against distinct values. * **Cons:** Can become less readable if you have a large number of cases. It's not ideal for complex conditional logic that involves logical operators or nested conditions. * **Chain of `if...else if` Statements:** * **Pros:** More flexible for complex conditions involving multiple checks, logical operators, etc. Easier to read for simple cases with few comparisons. * **Cons:** Can be slower than `switch` when you have many cases because each condition needs to be evaluated sequentially. **Other Considerations:** * **Compiler Optimizations:** Modern JavaScript engines often perform optimizations that can significantly impact the performance difference between `switch` and `if...else if`. * **Benchmarking Context:** The results of this benchmark are specific to the test environment (Chrome 65 on Linux) and may vary on different browsers, operating systems, or hardware. **Alternatives:** While not directly tested in this example: * **`Object`-Based Lookup:** Use an object where keys are the values you're checking for, and values are the associated code to execute. This can be more performant than `switch` or `if...else if` chains for a large number of cases. Let me know if you have any other questions!
Related benchmarks:
querySelectorAll foreach vs getElementsByClassName [].forEach.call()
querySelectorAll foreach vs getElementsByClassName [].forEach.call() 2
querySelectorAll vs getElementsByClassName Test
element.children vs element.querySelectorAll vs element.querySelectorAll(:scope)
array.from.map vs array.from with map
Comments
Confirm delete:
Do you really want to delete benchmark?