Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Switch vs javascript map
(version: 0)
Comparing performance of:
Switch vs Map
Created:
4 years 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; case 4: return true; case 5: return true; case 6: return true; case 7: return true; case 8: return true; case 9: return true; case 10: return true; }
Map
const input = 10; const LUT = new Map([ [0, true], [1, true], [2, true], [3, true], [4, true], [5, true], [6, true], [7, true], [8, true], [9, true], [10, true] ]) LUT.get(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:
7 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Switch
182529376.0 Ops/sec
Map
4175938.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is tested, the pros and cons of each approach, and other considerations. **Benchmark Definition** The benchmark consists of two test cases: `Switch` and `Map`. The script preparation code and HTML preparation code are empty, which means that no additional setup or HTML rendering is required for these tests. **Test Cases** 1. **Switch** The benchmark definition contains a `switch` statement with 10 cases, where each case returns `true`. The input value is set to `10`, which will not match any of the cases and will fall through to the default case (or undefined behavior). In this test case, we're comparing the performance of JavaScript's built-in `switch` statement. 2. **Map** The benchmark definition creates a new instance of `Map` with 11 entries (0-10) mapping to `true`. The test then uses the `get()` method on the `Map` object with an input value of `10`, which will return `true`. In this test case, we're comparing the performance of JavaScript's built-in `Map` data structure. **Options Compared** The benchmark is testing two options: 1. **Switch**: The traditional JavaScript `switch` statement. 2. **Map**: A specific implementation of a Map data structure using the `get()` method. **Pros and Cons of Each Approach** 1. **Switch**: * Pros: Simple, efficient, and widely supported in JavaScript engines. * Cons: Can be slower for large number of cases due to the overhead of the `switch` statement. 2. **Map**: * Pros: Fast, efficient, and scalable data structure for key-value pairs. * Cons: Requires more memory and has additional overhead compared to a traditional `switch` statement. **Other Considerations** * The benchmark does not account for caching or memoization of the `switch` statement results. In practice, the result of a `switch` statement might be cached to avoid repeated evaluations. * The `Map` data structure is used here as an example of a key-value pair implementation, but there are other map-like data structures available in JavaScript, such as `Object` or `SparseArray`. * The benchmark assumes that the input value will always match one of the cases in the `switch` statement. In practice, you might want to add additional logic to handle unknown inputs. **Libraries and Special JS Features** In this benchmark, there are no external libraries used, but the use of the `Map` data structure implies knowledge of JavaScript's built-in data structures. No special JavaScript features or syntax are used in these tests.
Related benchmarks:
Array.prototype.map vs Lodash map
Array.from(NodeList).map vs [...NodeList].map vs map.call(NodeList) vs. NodeList#map copied from Array
Array.from(NodeList).map vs [...NodeList].map vs map.call(NodeList) vs. NodeList#map copied from Array vs for loop
Switch vs Object Literal vs If Else vs Map - testing with simpler data again again
Array.from().map vs Array.prototype.map.call
Comments
Confirm delete:
Do you really want to delete benchmark?