Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map.get vs Object property access2
(version: 1)
Comparing performance of:
Map vs Object vs Object2
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
obj = Object.create(null) obj['foo'] = 'bar' obj2 = { foo: 'bar' } map = new Map(Object.entries(obj))
Tests:
Map
map.get("foo")
Object
obj["foo"]
Object2
obj2["foo"]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Map
Object
Object2
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/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map
105341608.0 Ops/sec
Object
61984524.0 Ops/sec
Object2
92332000.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark tests the performance of accessing values in different structured data types in JavaScript: a `Map` and two `Object` instances. Specifically, it compares three different methods of value retrieval: 1. **Map.get**: Retrieving a value from a `Map` using the `.get()` method. 2. **Object property access**: Retrieving a value from a plain `Object`. 3. **Object property access through a different `Object` instance**. Here's a breakdown of each component: ### Options Compared: - **Map**: `map.get("foo")` - **Object**: `obj["foo"]` - **Object2**: `obj2["foo"]` ### Key Data Structures: 1. **Map**: - Created using `new Map(Object.entries(obj))`. - Maps are ordered collections of key-value pairs, where keys can be any type of value (not just strings or symbols as with Objects). This allows for more flexibility in key usage. - Performance-wise, Maps generally have better performance characteristics for frequent additions and removals (insertions, deletions) and maintain order of insertion. 2. **Object**: - `obj` is created using `Object.create(null)`, which creates an object without a prototype, meaning it does not inherit properties from `Object.prototype`. This can avoid issues like accidental access of properties that are not defined on the instance. - `obj2` is a standard object with a property `foo`. ### Pros and Cons: - **Map Pros**: - Flexible key types (can use objects as keys). - Maintains insertion order of keys — useful when order is important. - Better suited for scenarios requiring frequent additions and deletions. - **Map Cons**: - Generally has a higher memory overhead compared to plain objects. - Slightly slower for simple lookups compared to Objects when the keys are basic types (e.g., strings). - **Object Pros**: - Simple and straightforward for basic key-value pairs. - More memory-efficient for a lower number of properties. - Fast for simple access when using string keys. - **Object Cons**: - Limited to string and symbol types as keys. - Can inherit unwanted properties (unless `Object.create(null)` is used). - Does not maintain order of properties in all cases, making it less predictable for ordered access. ### Benchmark Results: The benchmark results show that accessing values has notable performance differences: 1. **Map** (`map.get("foo")`): - Executions per second: **105,341,608** 2. **Object** (`obj["foo"]`): - Executions per second: **61,984,524** 3. **Object2** (`obj2["foo"]`): - Executions per second: **92,332,000** From the results, it’s clear that using `Map` can be significantly faster than regular object property access in this test scenario, which can be valuable information when performance is critical. ### Other Considerations: - The choice between `Map` and `Object` often depends on the specific use case and requirements regarding both performance and functionality. If you need to use complex key types or maintain order, `Map` would be preferable, while for straightforward CRUD operations on string keys, a regular `Object` may suffice. - It's also worth mentioning that with the ES6 and later syntax, there have been improvements in the language that can help mitigate some of the issues associated with traditional `Object` usage, such as using `Object.entries()`, `Object.keys()`, and others. Depending on performance needs and operational patterns, software engineers can weigh these options to determine the best approach for their respective applications.
Related benchmarks:
Map vs Object
Object access by key vs Map.get
Map vs Objectasdfasdfasdfasdf
keys VS map
hasOwnProperty vs Map()
Key lookup using different methods.
entries vs keys lookup
Map.set vs Object assign
Map.get vs Object property access
Comments
Confirm delete:
Do you really want to delete benchmark?