Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs switch soh
(version: 0)
Whats faster a map or a switch statement???
Comparing performance of:
Map vs Switch
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const map = { one: 1, two: 2, three: 3, four: 4, five: 5, six: 6, seven: 7, eight: 8, nine: 9, ten: 10, } function byMap(val) { return map[val] } function bySwitch(val) { switch (val) { case 'one': return 1; case 'two': return 2; case 'three': return 3; case 'four': return 4; case 'five': return 5 case 'six': return 6 case 'seven': return 7 case 'eight': return 8 case 'nine': return 9 case 'ten': return 10 } }
Tests:
Map
byMap('one') byMap('two') byMap('three') byMap('four') byMap('five') byMap('six') byMap('seven') byMap('eight') byMap('nine') byMap('ten')
Switch
bySwitch('one') bySwitch('two') bySwitch('three') bySwitch('four') bySwitch('five') bySwitch('six') bySwitch('seven') bySwitch('eight') bySwitch('nine') bySwitch('ten')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map
Switch
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36 Edg/137.0.0.0
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map
178672800.0 Ops/sec
Switch
71022816.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what is being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark definition represents a JavaScript function that maps a value to another value using an object (`map` in this case). The `byMap` function takes a value as input and returns the corresponding value from the `map` object. A similar function is defined using a `switch` statement (`bySwitch`). **Options Compared** Two options are being compared: 1. **Map**: This option uses an object (`map`) to store key-value pairs, where the key is the input value, and the value is the corresponding output value. 2. **Switch**: This option uses a `switch` statement with multiple cases to determine the output value based on the input value. **Pros and Cons** **Map:** Pros: * Easier to add or remove mappings (key-value pairs) * Can be more efficient for large datasets if implemented correctly * Less prone to performance issues due to branch prediction Cons: * May require additional memory to store the object * Can be slower due to the overhead of creating and accessing the object **Switch:** Pros: * Often faster due to the compiler's ability to predict branch outcomes * Less memory-intensive, as only a single variable is needed * Simplifies maintenance and updates for existing cases Cons: * Requires more code to add or remove cases * Can be slower if the number of cases is large, leading to slower performance over time **Library Used** In this benchmark, there are no explicitly mentioned libraries. However, it's worth noting that `map` and `switch` are built-in JavaScript constructs. **Special JS Features/Syntax** There doesn't appear to be any special JavaScript features or syntax being used in these benchmarks. They focus on comparing the performance of two fundamental control flow mechanisms (map and switch). **Alternative Approaches** If you're interested in exploring alternative approaches, some other options could include: 1. **Arrays**: Instead of using a map object, you could use an array to store values and their indices. 2. **Loops**: You could also implement this benchmark using loops, which would eliminate the need for objects or switch statements. 3. **Functions with early returns**: Another approach would be to use functions that take advantage of early returns, where the function returns immediately after evaluating a condition. Keep in mind that these alternatives might not provide the same level of performance as the original map and switch implementations, but they could offer different trade-offs in terms of complexity, readability, or maintainability.
Related benchmarks:
Switch vs map with string keys
Switch vs Map object
Switch vs map (100) 3
map vs ifelse vs switch test
Comments
Confirm delete:
Do you really want to delete benchmark?