Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Get from object vs get from map
(version: 1)
Comparing performance of:
Object vs Map
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Object
let obj = {}; for (let i = 0; i < 10000; i++) { obj[i] = i; } let out = new Array(100); for (let t = 0; t < 100; t++) { const idx = Math.floor(Math.random() * 10000); out[t] = obj[idx] }
Map
let map = new Map(); for (let i = 0; i < 10000; i++) { map.set(i, i); } let out = new Array(100); for (let t = 0; t < 100; t++) { const idx = Math.floor(Math.random() * 10000); out[t] = map.get(idx) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object
Map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object
22261.9 Ops/sec
Map
4229.3 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark presented in the JSON focuses on comparing two different data structures in JavaScript: an object (`Object`) and a `Map`. The goal is to measure the performance of retrieving values from these two types of collections when populated with the same set of data. ### Test Cases Explained 1. **Object Test**: - **Code**: ```javascript let obj = {}; for (let i = 0; i < 10000; i++) { obj[i] = i; } let out = new Array(100); for (let t = 0; t < 100; t++) { const idx = Math.floor(Math.random() * 10000); out[t] = obj[idx]; } ``` - **Explanation**: This test initializes an empty JavaScript object and adds 10,000 properties to it, where each property key is a number from `0` to `9999`, with the value being the same number. It then retrieves 100 random properties from the object and stores them in an output array. 2. **Map Test**: - **Code**: ```javascript let map = new Map(); for (let i = 0; i < 10000; i++) { map.set(i, i); } let out = new Array(100); for (let t = 0; t < 100; t++) { const idx = Math.floor(Math.random() * 10000); out[t] = map.get(idx); } ``` - **Explanation**: Similar to the object test, this snippet initializes a `Map` and sets 10,000 key-value pairs where keys and values are the same as in the object test. It then retrieves 100 random entries from the map and stores them in an output array. ### Performance Results The benchmark results showed: - **Object**: 22,328.30 executions per second - **Map**: 2,336.57 executions per second ### Pros and Cons #### Object - **Pros**: - Generally faster for accessing properties — as observed in the benchmark results. - Simple and lightweight syntax for defining key-value pairs. - **Cons**: - Property keys are always strings (or Symbols). While this is not a limitation in many cases, it can lead to confusion if numeric keys are used. - Less flexibility with key types; does not handle complex keys out-of-the-box. #### Map - **Pros**: - Can use any value (including objects) as keys, allowing for greater flexibility. - Maintains the order of insertion, which can be useful for applications where order matters. - **Cons**: - Generally slower for property access compared to an object (as shown in the benchmark). - Slightly more overhead due to maintaining more complex data structures. ### Other Considerations - **Use Cases**: The choice between using an `Object` or a `Map` depends on specific use cases. For instance: - If your application requires frequent value lookups with numerical keys and performance is critical, `Object` might be preferable. - If you're dealing with more complex key types or need to handle key order, `Map` would be the better choice. - **Alternatives**: Other data structures in JavaScript could be considered based on requirements: - **WeakMap**: Similar to `Map` but allows for garbage collection of keys that are no longer in use, which can prevent memory leaks when storing object references. - **Set**: If you need to store unique values without keys, this structure would be an alternative. - **Array**: For ordered collections of values that may need to be iterated through or sorted. Ultimately, the preference for one data structure over the other will depend on the specific requirements of the application, balancing between performance needs and the flexibility offered by each structure.
Related benchmarks:
Large Map vs Object 2
Map.get vs Object[i] vs Map.has
map vs object - key access 3
Map vs Object 5000 rand
Map.set() vs Object assignment in loops
map vs object - key access (string key)
map vs obj vs array
map vs obj vs array 2
Read Map vs Object
Comments
Confirm delete:
Do you really want to delete benchmark?