Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
condition if-else switch map
(version: 0)
Comparing performance of:
switch vs if-else vs map
Created:
4 years ago
by:
Registered User
Go to the latest result
Tests:
switch
function test(a) { switch(a) { case 0: return 0; case 1: return 0; case 2: return 0; case 3: return 0; case 4: return 0; case 5: return 0; case 6: return 0; case 7: return 0; case 8: return 0; case 9: return 0; case 10: return 0; case 11: return 0; case 12: return 0; default: return 0; } } test(12)
if-else
function test(a) { if (a === 0) { return 0; } else if (a === 1) { return 0; } else if (a === 2) { return 0; } else if (a === 3) { return 0; } else if (a === 4) { return 0; } else if (a === 5) { return 0; } else if (a === 6) { return 0; } else if (a === 7) { return 0; } else if (a === 8) { return 0; } else if (a === 9) { return 0; } else if (a === 10) { return 0; } else if (a === 11) { return 0; } else if (a === 12) { return 0; } else { return 0; } } test(12)
map
function test(a) { const map = new Map([ [0,0], [1,0], [2,0], [3,0], [4,0], [5,0], [6,0], [7,0], [8,0], [9,0], [10,0], [11,0], [12,0], ]) return map.get(a) || 0 } test(12)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
switch
if-else
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
switch
1136.4M/s
if-else
1115.0M/s
map
3.4M/s
View exact numbers
Test name
Executions per second
✓
switch
1,136,350,720 Ops/sec
if-else
1,115,009,536 Ops/sec
map
3,435,309 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Definition:** The benchmark is defined as a set of three test cases that compare different approaches to handle conditional logic: 1. **Switch statement**: A switch statement with multiple cases, where each case has the same return value (0). 2. **If-else statement**: An if-else statement with multiple conditions and return values. 3. **Map lookup**: Using a Map data structure to store values and retrieve them based on a key. **Test Cases:** Each test case has a unique name, description, script preparation code, and HTML preparation code (which is empty in this case). The script preparation code defines the function `test(a)` that will be executed for each test case. The input value `a` determines which branch of the conditional logic to take. **Switch Statement:** In the switch statement, the function `test(a)` takes the input `a` and returns 0 based on the following cases: * Case 0-12: Return 0 (no need for specific code) * Default case: Return 0 This implementation is straightforward but has a significant number of cases, which can make it slower than other approaches. **If-Else Statement:** In the if-else statement, the function `test(a)` takes the input `a` and returns 0 based on multiple conditions: * If `a === 0`, return 0 * Else if `a === 1`, return 0 * ... * Else if `a === 12`, return 0 * Default case: Return 0 This implementation is more straightforward than the switch statement but has a larger number of conditions, which can make it slower. **Map Lookup:** In the map lookup, the function `test(a)` takes the input `a` and returns 0 by looking up the value in the Map: * Create a Map with key-value pairs: `[0, 0], [1, 0], ..., [12, 0]` * Use the `get()` method to retrieve the value associated with the input `a`, or return 0 if not found This implementation is more concise and efficient than the switch statement or if-else statement. **Pros and Cons:** Here's a summary of the pros and cons for each approach: * **Switch Statement:** * Pros: * Easy to read and understand * Can be faster due to direct branching * Cons: * Has a large number of cases, making it slower * May not be suitable for larger numbers of cases * **If-Else Statement:** * Pros: * More straightforward than switch statements * Can be faster due to less branching * Cons: * Has a large number of conditions, making it slower * May not be suitable for very large numbers of conditions * **Map Lookup:** * Pros: * More concise and efficient than switch statements or if-else statements * Can handle larger numbers of cases without significant performance degradation * Cons: * May require more memory to store the Map **Other Alternatives:** Other alternatives for handling conditional logic include: * **Array lookups**: Instead of using a Map, you can use an array with a single value and check if the input `a` matches the value. * **Object lookups**: Similar to Map lookup, but uses objects instead of key-value pairs. * **Recursive functions**: You can implement recursive functions that handle conditional logic, which can be more concise than switch statements or if-else statements. Keep in mind that the best approach depends on the specific use case and performance requirements.
Related benchmarks
Switch vs If else CNC
Map vs switch soh
map vs ifelse vs switch test
map vs if/else vs switch
Switch vs Object Literal vs If Else vs Map - testing with simpler data again again
Comments
Confirm delete:
Do you really want to delete benchmark?