Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map VS Set by Tonkhao
(version: 7)
Comparing performance of:
createMap Middle vs createSet Middle vs createMap small vs createSet small vs createMap BIG vs createSet BIG vs remove from array vs remove from map vs remove from set
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const myMap = new Map() for (let i = 0; i++; i < 1000000) { myMap.set(i,i) } const mySet = new Map() for (let i = 0; i++; i < 1000000) { mySet.add(i) } let myArray = [] for (let i = 0; i++; i < 1000000) { myArray.push(i) } function createMap(size) { const myMap = new Map() for (let i = 0; i++; i < size) { myMap.set(size, size) } return myMap } function createSet(size) { const mySet = new Set() for (let i = 0; i++; i < size) { mySet.add(size, size) } return mySet } function removeFromArray(element) { return myArray.splice(myArray.indexOf(element), 1) } function removeFromMap(element) { return myMap.delete(element) } function removeFromSet(element) { return mySet.delete(element) }
Tests:
createMap Middle
createMap(9999)
createSet Middle
createSet(9999)
createMap small
createMap(10)
createSet small
createSet(10)
createMap BIG
createMap(1000000)
createSet BIG
createSet(1000000)
remove from array
removeFromArray(5000)
remove from map
removeFromMap(5000)
remove from set
removeFromSet(5000)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (9)
Previous results
Fork
Test case name
Result
createMap Middle
createSet Middle
createMap small
createSet small
createMap BIG
createSet BIG
remove from array
remove from map
remove from set
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of Map vs Set benchmarks. **What is tested?** The provided benchmark tests the performance of two data structures: Maps and Sets, both implemented using JavaScript objects. The benchmark evaluates how efficiently these data structures can be created, populated with elements, and iterated over to remove specific elements. **Options compared** There are four options being compared: 1. **Map Creation**: Creating a Map instance and populating it with 1 million elements. 2. **Set Creation**: Creating a Set instance and adding 1 million elements using the `add()` method. 3. **Array Creation**: Creating an array of 1 million elements using the `push()` method. 4. **Remove from Array, Map, and Set**: Removing specific elements (5000) from each data structure. **Pros and Cons of different approaches** * **Map Creation**: Maps are more flexible than Sets when it comes to storing arbitrary key-value pairs. However, this flexibility comes at a cost: Maps have a slower creation time compared to Sets. * **Set Creation**: Sets are optimized for fast membership testing and element addition. They have a faster creation time compared to Maps but may be less suitable for applications requiring arbitrary keys. * **Array Creation**: Creating an array is the simplest approach, with the fastest execution time. However, arrays lack the flexibility of Maps and Sets when it comes to storing data. **Remove from Array, Map, and Set** Removing elements from each data structure has a different performance profile: * **Array Removal**: Removing an element from an array using `splice()` or indexing is relatively fast. * **Map and Set Removal**: Removing an element from a Map or Set using the `delete()` method or by iterating over the keys/values is slower compared to array removal. **Observations** The benchmark results show that: * Creating a Set is the fastest approach, especially for large datasets. * Maps have a slower creation time compared to Sets but may be more suitable for applications requiring flexible key-value pairs. * Array creation is the simplest approach with the fastest execution time. * Removing elements from an array is relatively fast, while Map and Set removal are slower. **Conclusion** The choice of data structure depends on the specific use case: * Use a Set when you need to store unique values and perform fast membership testing. * Use a Map when you need to store arbitrary key-value pairs and require flexibility. * Use an array when simplicity and speed are more important than flexibility.
Related benchmarks:
For loop map vs map builtin for 10000000 elements
Array from() vs Map.keys()
Array from() vs Map.keys() vs Map.values() vs spread
Array from() vs Map.keys() vs Map.values() vs spread (fixed)
Comments
Confirm delete:
Do you really want to delete benchmark?