Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Compare switch vs dictionary
(version: 0)
Comparing performance of:
switch vs dictionary
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var random = Math.floor(Math.random() * 20); var result = ""; var test = { "0": "something else", "1": "something else", "2": "something else", "3": "something else", "4": "something else", "5": "something else", "6": "something else", "7": "something else", "8": "something else", "9": "something else", "10": "something else", "11": "something else", "12": "something else", "13": "something else", "14": "something else", "15": "something else", "16": "something else", "17": "something else", "18": "something else", "19": "something else", "20": "something else" }; function getValue(random) { switch (random) { case "0": return "something else"; case "1": return "something else"; case "2": return "something else"; case "3": return "something else"; case "4": return "something else"; case "5": return "something else"; case "6": return "something else"; case "7": return "something else"; case "8": return "something else"; case "9": return "something else"; case "10": return "something else"; case "11": return "something else"; case "12": return "something else"; case "13": return "something else"; case "14": return "something else"; case "15": return "something else"; case "16": return "something else"; case "17": return "something else"; case "18": return "something else"; case "19": return "something else"; case "20": return "something else"; default: return "something else"; } };
Tests:
switch
getValue(random);
dictionary
test[random];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
switch
dictionary
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:137.0) Gecko/20100101 Firefox/137.0
Browser/OS:
Firefox 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
switch
37389272.0 Ops/sec
dictionary
1391940736.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark test case and explain what's being tested. **Benchmark Definition:** The benchmark is comparing the performance of two approaches: 1. `switch` statement 2. Dictionary (object) lookup The script preparation code creates an object `test` with 20 properties, each assigned a string value. The `getValue` function uses either a `switch` statement or dictionary lookup to return the corresponding string value based on a randomly generated number. **Options being compared:** * **Switch statement**: This approach uses a traditional `switch` statement to match the input number against a series of cases. * **Dictionary (object) lookup**: This approach uses an object (dictionary) to map the input number to its corresponding string value. The dictionary is created in the script preparation code. **Pros and Cons:** * **Switch statement**: + Pros: Can be more readable for certain use cases, especially when working with fixed-size sets of cases. + Cons: Can be slower due to the overhead of the switch statement mechanism, and may not perform as well for large numbers of cases. * **Dictionary (object) lookup**: + Pros: Typically faster than a switch statement, since it allows for O(1) lookups. It's also more flexible and can handle arbitrary key-value pairs. + Cons: May require more memory to store the dictionary, especially for large datasets. **Library used:** None explicitly mentioned in the benchmark definition. However, the `Math.random()` function is used to generate a random number, which suggests that JavaScript's built-in random number generator is being used. **Special JS feature or syntax:** The use of a dictionary (object) lookup is an example of JavaScript's built-in object-oriented programming features. This syntax is widely supported in modern JavaScript environments and is often preferred for its performance and readability benefits. **Other considerations:** * **Cache locality**: Both approaches rely on cache locality, where the CPU can access data that's already stored in the cache. The switch statement may have an advantage here, as it can reuse the same memory locations for each case. * **Branch prediction**: Modern CPUs often use branch prediction to guess which branch will be taken based on the input. In this case, the dictionary lookup might benefit from better branch prediction, since it's a sequential operation. **Alternatives:** Other approaches that could be used to compare performance include: * Using an array or array-like object instead of a dictionary * Employing a different data structure, such as a hash table or trie * Using a third-party library or framework for testing and benchmarking Keep in mind that these alternatives might not provide the same level of performance insight as using an actual JavaScript switch statement or dictionary lookup.
Related benchmarks:
lodash test
lodash test
lodash test
lodash test
lodash test
Comments
Confirm delete:
Do you really want to delete benchmark?