Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test Map vs Object with string which use string as key and value
(version: 1)
Comparing performance of:
Map get vs Obj get vs Map set vs Obj set
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const map = new Map(); const obj = {}; function generateKeys(startIndex, endIndex) { const res = []; for (let i = startIndex; i < endIndex; i++) { res.push(btoa(i)); } return res; } const kvs = generateKeys(0, 1000); const newKvs = generateKeys(10000, 12000); kvs.forEach(kv => { map.set(kv, kv); obj[kv] = kv; });
Tests:
Map get
kvs.forEach(kv => { map.get(kv); });
Obj get
kvs.forEach(kv => { obj[kv]; });
Map set
kvs.forEach(kv => { map.set(kv, kv); });
Obj set
kvs.forEach(kv => { obj[kv] = kv; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Map get
Obj get
Map set
Obj set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:138.0) Gecko/20100101 Firefox/138.0
Browser/OS:
Firefox 138 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map get
76536.3 Ops/sec
Obj get
15131.3 Ops/sec
Map set
11818.8 Ops/sec
Obj set
12512.8 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark on MeasureThat.net compares the performance of JavaScript Maps versus JavaScript Objects when using strings as keys and values. The benchmarks measure the efficiency of "get" and "set" operations for both data structures. ### Options Compared 1. **Map** - **Test Cases**: - `Map get`: Retrieving values using the `get()` method. - `Map set`: Storing values using the `set()` method. 2. **Object** - **Test Cases**: - `Obj get`: Accessing properties directly through the bracket notation. - `Obj set`: Assigning properties directly using bracket notation. ### Performance Results From the benchmark results provided, there are observable execution speeds for each operation (measured in executions per second): - **Map get**: 281,959.72 - **Obj get**: 82,334.77 - **Obj set**: 68,567.32 - **Map set**: 46,790.65 ### Pros and Cons #### Map **Pros**: - Maps have better performance for the `get` operation compared to objects, as seen in the results. This is particularly advantageous when frequent retrievals are required. - Maps also maintain the order of entries, which can be crucial for certain use cases where the order of keys matters. - Maps accept any value (objects, functions, etc.) as keys, not just strings or symbols. **Cons**: - Set and retrieval speeds for Maps are generally slower than Objects when it comes to the `set` operation in this benchmark. - Maps can be more memory-intensive due to their more complex internal structure. #### Object **Pros**: - The direct access of properties via bracket notation is faster than Map set in this benchmark. - They are simple to use and have been part of JavaScript for a long time, making them widely understood and supported. - Objects use less memory compared to Maps. **Cons**: - Objects do not maintain order and may lead to inconsistent key ordering. - The restriction of using only strings and symbols as keys limits flexibility in certain scenarios where a variety of types may be required. ### Other Considerations - **Use Case Suitability**: If key order matters or if non-string keys are used frequently, Maps are preferable. For scenarios where key values are limited to strings and performance in sets is more critical, Objects might be a better choice. - **Memory Considerations**: While performance is a crucial metric, also take into account the memory usage based on the expected dataset size and structure. ### Alternatives 1. **WeakMap**: Similar to Map, but with keys that are weakly referenced, meaning they can be garbage collected. This is useful for caching without preventing memory cleanup. 2. **Set/WeakSet**: If the use case involves storing unique values and ensuring no duplicates, Set may be preferable over both Map and Object. 3. **Plain Objects**: In scenarios primarily dealing with JSON-like structures without needing advanced features of Maps, plain objects can suffice. In conclusion, this benchmark provides significant insights into the performance trade-offs between Maps and Objects in JavaScript for specific operations. Understanding these nuances enables software engineers to make optimized choices based on their application's needs.
Related benchmarks:
for-in vs object.keys v3
Access Object, Map, Set
Map vs Object (2)
Map vs Object (3)
Object vs Map with string key
Map vs Obj Lookup
Map vs Object - Multiple keys
Map.has vs Object hasOwnProperty
Map.has vs Object hasOwnProperty vs direct check v2
Comments
Confirm delete:
Do you really want to delete benchmark?