Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
switch vs dictionary lookup
(version: 0)
Performance comparison between javascript switch and a dictionary lookup for a simple string return
Comparing performance of:
Switch vs Dict
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var getCursorClass = (state) => { switch (state) { case 'pending': return 'cursor-wait'; case 'disabled': return 'cursor-not-allowed'; default: return 'cursor-pointer'; } } var cursorClassDict = { 'pending': 'cursor-wait', 'disabled': 'cursor-not-allowed', 'default': 'cursor-pointer' } var states = ['pending','disabled', 'default'];
Tests:
Switch
const state = states[Math.floor(Math.random()*states.length)]; return getCursorClass(state)
Dict
const state = states[Math.floor(Math.random()*states.length)]; return cursorClassDict[state]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Switch
Dict
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36 Edg/146.0.0.0
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Switch
104059992.0 Ops/sec
Dict
44367268.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is testing the performance difference between two approaches: 1. **Switch statement**: The `switch` statement is used to execute different blocks of code based on the value of an expression. 2. **Dictionary lookup**: A dictionary (object) is used to store key-value pairs, where the key is a string and the value is another string. **Script Preparation Code** The script preparation code defines two functions: 1. `getCursorClass(state)` uses a `switch` statement to return a cursor class based on the input `state`. 2. `cursorClassDict` is an object that maps states to cursor classes, with the same keys as in the `switch` statement. **Html Preparation Code** There is no HTML preparation code provided. **Individual Test Cases** The benchmark has two test cases: 1. **Switch**: The script uses the `getCursorClass(state)` function with a random state value selected from an array of states. 2. **Dict**: The script uses the `cursorClassDict[state]` expression to retrieve the cursor class for a random state value. **Library and Purpose** The `Math.random()` function is used to generate random numbers, which in this case is used to select a random state value. **Special JS Features or Syntax** None are mentioned explicitly in the benchmark code. However, it's worth noting that the use of an array of states (`states`) is a modern JavaScript feature (ECMAScript 2015+), although not particularly complex or performance-critical. **Pros and Cons of Different Approaches** 1. **Switch Statement**: * Pros: Can be more readable for simple cases, allows for more explicit control flow. * Cons: Can lead to longer execution paths and slower performance compared to dictionary lookups. 2. **Dictionary Lookup**: * Pros: Faster lookup times, can lead to better cache locality. * Cons: May require more code and planning to set up the dictionary. **Other Considerations** The benchmark is focused on the performance difference between these two approaches. Other factors that might influence performance include: * The size of the state array (`states`) * The number of iterations or loops in the script * Any other operations or computations performed before or after the switch statement and dictionary lookup **Alternative Approaches** Other alternatives to consider for similar benchmarking scenarios include: 1. **Array.prototype.indexOf()**: This method can be used to find the index of a state value in the `states` array, which might provide comparable performance to the dictionary lookup. 2. **Regular expressions**: If the states are represented as strings with specific patterns, regular expressions could be used to match and retrieve the corresponding cursor class. 3. **Object.keys() and Array.prototype.indexOf() combination**: This approach uses the `Object.keys()` method to get an array of state values and then uses `Array.prototype.indexOf()` to find the index of a random state value. Keep in mind that these alternatives might not provide identical performance characteristics as the original dictionary lookup or switch statement approaches, but they could offer interesting variations on the benchmarking theme.
Related benchmarks:
undefined vs. hasOwnProperty2
hasOwnProperty string vs number
undefined vs evaluation vs hasOwnProperty
Array includes vs Object key lookup vs Object hasOwnProperty
Switch/case vs indexOf
Comments
Confirm delete:
Do you really want to delete benchmark?