Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object vs Set add
(version: 0)
Comparing performance of:
m vs o vs s
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var map = new Map(); var obj = {}; var set = new Set(); var ary = []; for (let i=0; i<10000; i++) { ary.push(Math.random()); }
Tests:
m
for (let a of ary) { map.set(a, true); }
o
for (let a of ary) { obj[a] = true; }
s
for (let a of ary) { set.add(a); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
m
o
s
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 break down the provided benchmark and explain what is being tested, compared, and other considerations. **Benchmark Overview** The benchmark tests the performance of three data structures: `Map`, `Object`, and `Set`. Each data structure has an identical setup: an array `ary` with 10,000 random elements. The test cases then perform a simple operation on each data structure: 1. `m`: Iterating over the array using a `for...of` loop and setting each element in the `Map`. 2. `o`: Iterating over the array using a `for...of` loop and setting each element as a property of an empty `Object`. 3. `s`: Adding each element to a new `Set`. **Comparison** The benchmark compares the performance of these three approaches: * **Map**: Using a `Map` to store elements, where each key is unique and has a corresponding value. * **Object**: Using an `Object` with dynamic properties, similar to a hash table. However, unlike a Map, there are no guarantees that each property will be unique. * **Set**: Using a `Set`, which is designed for fast membership testing and insertion of unique elements. **Pros and Cons** Here's a brief summary: * **Map**: * Pros: Fast lookups (O(1) average case), can handle duplicate keys, and has a more JavaScript-like syntax. * Cons: May have higher memory usage due to the extra overhead of storing keys as strings, even if they're not unique. * **Object**: * Pros: Similar to a Map in terms of performance, but with less memory usage since property names are not stored separately. * Cons: Less predictable than a Map when dealing with duplicate properties, and less JavaScript-like syntax (since you can't have multiple keys per object). * **Set**: * Pros: Fast membership testing (O(1)) and insertion of unique elements. It's also very memory-efficient since it doesn't store any keys or values. * Cons: More complex to use for simple lookups, as you'd need to check if an element is in the Set. **Library Usage** The test case uses a `Set` library function (not explicitly stated but implied by its behavior). The `Set` data structure is built-in to JavaScript and doesn't require any external libraries. However, there are other alternatives to `Set` that can be used depending on the situation: * **Array**: Using an array with unique elements can provide similar performance benefits as a Set. However, it may not be as efficient for very large datasets since arrays require more memory and have slower membership testing. * **Hash Table** libraries: There are third-party JavaScript libraries that implement hash tables (similar to Maps) but might offer better performance or features than the built-in `Map` data structure. There's no special JavaScript feature or syntax being used in this benchmark, aside from some minor differences between `for...of` loops and using object properties. **Other Considerations** When choosing a data structure for your use case: * **Memory usage**: If memory is limited, consider using an array or a lightweight hash table. * **Performance**: Choose a data structure that balances performance with the frequency of insertions, lookups, and deletions. * **Complexity**: Select a data structure based on how complex your use case is. For instance, if you need to store and retrieve values frequently but don't care about unique keys or membership testing, an object might be sufficient. Keep in mind that this benchmark is focused on the performance of specific operations (adding elements, lookups) rather than the overall behavior or features of each data structure.
Related benchmarks:
Map vs Array vs Object set uint32 key speed
Map vs Array vs Object has uint32 key speed
Map vs Array vs Object vs Set add item speed in 50000 iters 2
Map vs Object set value
Array vs Class
Comments
Confirm delete:
Do you really want to delete benchmark?