Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object - abcdefg
(version: 0)
Lookup of map vs object
Comparing performance of:
Map lookup vs Obj lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var map = new Map(); var obj = {}; map.set('abcdefg', 5); obj['abcdefg'] = 5; var i = 0, count = 1000, a;
Tests:
Map lookup
for (i = 0; i < count; i++) { a = map.get('abcdefg'); }
Obj lookup
for (i = 0; i < count; i++) { a = obj['abcdefg']; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map lookup
Obj lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map lookup
9990.7 Ops/sec
Obj lookup
10269.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain the benchmark and its results. **Benchmark Overview** The benchmark compares the performance of looking up values in two different data structures: `Map` and `Object`. The test cases are designed to measure the execution time of accessing a value in each data structure using the `.get()` method for `Map` and bracket notation (`[key]`) for `Object`. **Options Compared** The benchmark compares the performance of: 1. **Map lookup**: Using the `.get()` method on a `Map` object to retrieve a value. 2. **Object lookup**: Using bracket notation (`[key]`) on an `Object` to retrieve a value. **Pros and Cons of Each Approach** **Map Lookup:** Pros: * Can be faster than Object lookup when the key is consistently present in the map, as it avoids the need for string comparison. * Provides better performance for large datasets with many keys. Cons: * Requires additional overhead to create and manage a Map object. * The `.get()` method can throw an error if the key is not present in the map, whereas bracket notation on an Object will return `undefined` instead. **Object Lookup:** Pros: * Does not require creating or managing a separate data structure (the Map). * Bracket notation is a common and familiar way to access properties in JavaScript objects. * Returns `undefined` if the key is not present, which can be useful for some use cases. Cons: * Can be slower than Map lookup when the key is consistently present, due to the need for string comparison. * Requires more overhead due to the need for type checking and coercion (e.g., converting strings to symbols). **Library Used** The benchmark uses the `Map` data structure, which is a built-in JavaScript object. The `Map` API provides an efficient way to store key-value pairs and retrieve values using the `.get()` method. No external libraries are used in this benchmark. **Special JS Features or Syntax** This benchmark does not use any special JavaScript features or syntax beyond what's typically available in modern browsers. However, it's worth noting that some older browsers may have slightly different implementation details for `Map` and Object lookups. **Other Alternatives** If you're interested in exploring alternative data structures or lookup methods, here are a few options: * **Set**: Similar to `Map`, but without the ability to store arbitrary key-value pairs. * **WeakMap**: A variant of `Map` that only stores references to objects and not values themselves. This can be useful for optimizing memory usage in certain scenarios. * **Array**: While not ideal for lookup, arrays can provide a simple way to access elements using indices. However, this approach is generally slower than using `Map` or Object lookups. I hope this explanation helps!
Related benchmarks:
fdMap vs Objectasdfasdfasdfasdfdsfad
Array from() vs Map.keys()
Array from() vs Map.keys() vs Map.values() vs spread
Map vs Object read performance for a 1000 key lookup
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?