Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object vs Set lookup and add
(version: 2)
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) { if (!map.has(a)) map.set(a, true); }
o
for (let a of ary) { if (obj[a] === undefined) obj[a] = true; }
s
for (let a of ary) { if (!set.has(a)) 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):
**Benchmark Explanation** The provided benchmark tests the performance of three data structures in JavaScript: `Map`, `Object`, and `Set`. Specifically, it measures how fast these data structures can perform lookups and additions. **Options Compared** There are three test cases: 1. **`Map`**: The code uses a `Map` to store values with random keys. It checks if the key is already present in the map by using the `has()` method. If not, it sets the value to `true` using the `set()` method. 2. **`Object`**: The code uses an empty object (`{}`) to store values with random keys. It checks if the key is already present in the object by checking for the presence of a property on the object using the square bracket notation (`obj[key] === undefined`). If not, it sets the value to `true`. 3. **`Set`**: The code uses a `Set` to store unique values with random keys. It checks if the key is already present in the set by using the `has()` method. If not, it adds the value to the set. **Pros and Cons of Each Approach** * **`Map`**: + Pros: Fast lookups (O(1)) and insertions (O(1)) due to its hash-based implementation. + Cons: Requires more memory compared to `Object` or `Set` because it stores both key-value pairs and the hash function's output. * **`Object`**: + Pros: Lightweight, no additional memory is needed beyond what's already allocated for the object itself. + Cons: Slower lookups (O(n)) due to linear search through properties, which can be slow for large datasets. Insertions are also O(1), but may require more computational overhead due to potential rehashing of property names. * **`Set`**: + Pros: Fast insertions and lookups (O(1)) because it uses a hash-based data structure internally. + Cons: Not suitable for storing key-value pairs; can only store unique values. **Library and Special JS Feature** None are mentioned in the provided benchmark definition. **Other Considerations** When choosing between `Map`, `Object`, and `Set` for your use case: * If you need to frequently look up or add values with random keys, consider using a `Map`. * For applications requiring lightweight data storage without frequent key-value pair manipulation, `Object` might be sufficient. * When dealing with unique values only (e.g., set membership tests), `Set` is the most efficient choice. **Alternative Options** Other JavaScript data structures that could be used for similar benchmarks include: * `Array`: While not as efficient for lookups and insertions as `Map`, `Object`, or `Set`, it's a common choice when only storing ordered values. * `WeakMap`/`WeakSet`: Similar to `Map` and `Set`, but designed specifically for scenarios where the data structure isn't the primary focus (e.g., caching). * Custom implementations: Depending on your specific use case, implementing a custom data structure might provide optimal performance or features not available in built-in JavaScript data structures. The choice of which data structure to use ultimately depends on the requirements and constraints of your application.
Related benchmarks:
Map vs Array vs Object set uint32 key speed
Map vs Array vs Object has uint32 key speed
Object keys vs Array map v2
Map vs Object 5000 rand
Map vs Object set value
Comments
Confirm delete:
Do you really want to delete benchmark?