Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test switch vs map 2
(version: 1)
Comparing performance of:
Switch vs Map
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Switch
const input = 10; switch(input) { case 1: return true; case 2: return true; case 3: return true; default: return false; }
Map
const input = 10; const myMap = new Map([ [1, true], [2, true], [3, true] ]); if(myMap.has(input)){ return myMap[input]; } else { return false; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Switch
Map
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/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Switch
161755824.0 Ops/sec
Map
17941542.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
In the provided benchmark, the performance of two different control structures in JavaScript—`switch` statement and `Map` object—is being tested under the name "test switch vs map 2." The goal is to determine which approach performs better in terms of execution speed. ### Test Cases Compared 1. **Switch Statement** - **Code**: ```javascript const input = 10; switch(input) { case 1: return true; case 2: return true; case 3: return true; default: return false; } ``` - **Description**: The `switch` statement checks the value of `input` against fixed cases (1, 2, 3). If the value matches one of the cases, it returns `true`, otherwise it returns `false`. 2. **Map Object** - **Code**: ```javascript const input = 10; const myMap = new Map([ [1, true], [2, true], [3, true] ]); if(myMap.has(input)) { return myMap.get(input); } else { return false; } ``` - **Description**: The `Map` object is a collection of key-value pairs. This benchmark checks if the `input` exists as a key in the `myMap`, and if it does, returns the associated value (which is `true` for keys 1, 2, 3). If the `input` is not found, it returns `false`. ### Performance Results The test results show a significant difference in `ExecutionsPerSecond` between the two approaches: - **Switch**: 161,755,824 executions per second. - **Map**: 17,941,542 executions per second. ### Pros and Cons **Switch Statement** - **Pros**: - Generally faster for a small set of known values (as evidenced in this benchmark). - Readability can be good when handling simple conditions. - **Cons**: - Limited to checking equality; additional conditions or complex evaluations require more verbose logic or nested cases. **Map Object** - **Pros**: - More flexible; can store any type of value or object as keys and values. - Allows for non-numeric keys, and can be more efficient when the dataset is large and dynamic. - Provides built-in methods (like `has` and `get`) for these operations. - **Cons**: - In this benchmark, it performs worse than the `switch` statement for the specific scenario. - Slightly more overhead in terms of memory due to the object structure. ### Other Considerations 1. **Use Cases**: The optimal choice between `switch` and `Map` may depend on the specific use case. If you have a limited and fixed number of possibilities (like 1, 2, 3), then a `switch` may be preferable for speed. However, if you are managing a dynamic list of values, `Map` could be more suitable. 2. **Alternatives**: - **If-Else Chains**: An alternative could be using an `if-else` chain, which might be more readable in some scenarios, particularly when conditions are more complex. - **Object Lookup**: Another option could be an object where you use keys to represent possible inputs and values for outcomes, which could combine some benefits of both approaches. For example, `const outcomes = {1: true, 2: true, 3: true}; return outcomes[input] || false;`. - **Performance Trade-offs**: Trade-offs between readability, maintainability, and execution speed should be considered based on the specific requirements of the project and the expected data patterns. In summary, both techniques have their merits, and the choice between them should be informed by the particular requirements and context of the application being developed.
Related benchmarks:
Switch vs map
Switch vs new Map
Switch vs javascript map
Switch vs Map object
my test bench 1
Switch vs map test2
Switch vs object mapping
Switch vs object mapping 3
test switch vs map
Comments
Confirm delete:
Do you really want to delete benchmark?