Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map Vs Switch
(version: 2)
Comparing performance of:
Switch vs Map vs Hashmap
Created:
5 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<p>Test</p>
Script Preparation code:
function main() { console.log('it wont save if I dont put anything here') }
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 map = new Map([ ["1", true], ["2", true], ["3", true], ["4", true], ["5", true], ["6", true], ["7", true], ["8", true], ["9", true], ["10", true], ]); return map.get("10");
Hashmap
const map = { "1": true, "2": true, "3": true, "4": true, "5": true, "6": true, "7": true, "8": true, "9": true, "10": true, }; return map["10"]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Switch
Map
Hashmap
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
19 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36 Edg/147.0.0.0
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Switch
110428112.0 Ops/sec
Map
3752322.5 Ops/sec
Hashmap
102963936.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark named "Map Vs Switch". It consists of three individual test cases: `Switch`, `Hashmap` (which is actually an object, not a traditional Map), and `Map`. The benchmark definition is represented by a string that defines the code to be executed in each test case. **Script Preparation Code** The script preparation code is a simple JavaScript function named `main` that logs a message to the console. This code is executed before running each test case. **Html Preparation Code** The html preparation code consists of an HTML paragraph element `<p>Test</p>` that is rendered along with the test cases. **Individual Test Cases** There are three individual test cases: 1. **Switch** This test case defines a `switch` statement that takes a string input `"10"` and returns `true` for all possible inputs from 1 to 10. 2. **Hashmap (Object)** This test case creates an object with key-value pairs and retrieves the value associated with the key `"10"`. This is equivalent to using a traditional Map in JavaScript. 3. **Map** This test case creates a new Map instance, adds key-value pairs to it, and retrieves the value associated with the key `"10"`. **Options Compared** The benchmark compares the performance of three different approaches: 1. `Switch`: A traditional `switch` statement. 2. `Hashmap (Object)`: An object-based approach that uses bracket notation to access values. 3. `Map`: A traditional Map instance. **Pros and Cons** Here's a brief analysis of each approach: 1. **Switch**: Pros: * Simple and concise code * Fast lookup times due to the nature of the `switch` statement Cons: * Limited in terms of scalability and flexibility 2. **Hashmap (Object)**: Pros: * Easy to implement and understand * Works well for small datasets Cons: * Not as performant as traditional Maps, especially for large datasets 3. **Map**: Pros: * Fast lookup times due to the nature of the Map data structure * Scalable and flexible Cons: * Requires explicit key-value pair management **Other Considerations** When choosing between these approaches, consider the following: * If you need a simple, concise solution for small datasets, `Switch` might be sufficient. * For larger datasets or performance-critical applications, `Map` is likely a better choice. * When working with objects, `Hashmap (Object)` can be a convenient alternative to traditional Maps. **Special JS Feature** The test cases use bracket notation (`map[10]`) and object literal syntax (`{ "1": true }`). These are standard JavaScript features that have been around since the language's inception.
Related benchmarks:
Switch vs Object Literal1551511551
Object vs Switch..Case vs Map
map vs object vs switch dispatcher
map vs ifelse vs switch test
Switch vs Object Literal vs If Else vs Map - testing with simpler data again again
Comments
Confirm delete:
Do you really want to delete benchmark?