Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch vs map with string keys and early exit 2
(version: 0)
Comparing performance of:
Switch vs Map
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var runSwitch = input => { switch(input) { case 'one': return true; case 'two': return true; case 'three': return true; case 'four': return true; case 'five': return true; case 'six': return true; case 'seven': return true; case 'eight': return true; case 'nine': return true; case 'ten': return true; } } var LUT = { zero: true, one: true, two: true, three: true, four: true, five: true, six: true, seven: true, eight: true, nine: true, ten: true } var input = 'one';
Tests:
Switch
runSwitch(input);
Map
LUT[input];
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
8119621.5 Ops/sec
Map
8822216.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The benchmark is designed to compare two approaches: `switch` statements and using an object map (`LUT`) with early exit conditions. The test case uses a simple scenario where it takes an input string and checks if it matches one of 10 predefined values. **Options Compared** 1. **Switch Statement**: The first option, labeled "Switch", tests the performance of a traditional `switch` statement. This approach is straightforward but can be slower due to the overhead of branching instructions. 2. **Object Map (LUT)**: The second option, labeled "Map", uses an object map (`LUT`) with early exit conditions. This approach takes advantage of JavaScript's hash table implementation and can be faster than a traditional `switch` statement. **Pros and Cons** * **Switch Statement**: + Pros: Easy to understand and maintain. + Cons: Can be slower due to branching instructions. * **Object Map (LUT)**: + Pros: Can take advantage of JavaScript's hash table implementation for faster lookups. + Cons: Requires a predefined object with all possible values, which can be cumbersome if the number of cases is large. **Library and Its Purpose** The `LUT` (Lookup Table) object is used to store the precomputed results for each input value. This allows for fast lookups using the object's hash table implementation. The purpose of this library is to provide a way to speed up the comparison process by reducing the number of branching instructions. **Special JS Feature or Syntax** There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives** * **Regex**: Another approach to compare performance would be to use regular expressions (regex) to match the input string against a set of predefined values. This approach can be more flexible but may also be slower due to regex overhead. * **Array Lookups**: Using an array with `indexOf()` or `includes()` methods could be another alternative to compare performance. However, this approach would require creating an array with all possible values, which might be cumbersome if the number of cases is large. In summary, the benchmark compares two approaches: traditional `switch` statements and using an object map (`LUT`) with early exit conditions. The `LUT` approach takes advantage of JavaScript's hash table implementation for faster lookups but requires a predefined object with all possible values.
Related benchmarks:
Switch vs map with string keys
Switch vs map with string keys and early exit
Map vs switch soh
map vs ifelse vs switch test
Comments
Confirm delete:
Do you really want to delete benchmark?