Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object with 100 keys and mutate
(version: 1)
Lookup of map vs object
Comparing performance of:
Map set vs Obj set
Created:
7 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
var map = new Map(); var obj = {}; const nbKeys = 100; for(let n =0; n<nbKeys; n++) { const nKey = n; const nValue = Math.random(); map.set(nKey, nValue ); obj[nKey] = nValue ; } var i = 0, count = 1000, a; function getRandomInt(max) { return Math.floor(Math.random() * max); }
Tests:
Map set
for (i = 0; i < count; i++) { map.set(getRandomInt(nbKeys), getRandomInt(42)); }
Obj set
for (i = 0; i < count; i++) { obj[getRandomInt(nbKeys)] = getRandomInt(42); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map set
Obj set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/604.1.38 (KHTML, like Gecko) Chrome/49.0.2623 Safari/604.1.38 CoherentGT/2.0
Browser/OS:
Chrome 49 on Windows 8
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map set
4958.7 Ops/sec
Obj set
5268.3 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 7 months ago):
The benchmark defined on MeasureThat.net compares the performance of two different data structures in JavaScript: `Map` and plain JavaScript `Object`. Specifically, it focuses on the performance of setting (mutating) values in both data structures when dealing with 100 keys. ### Benchmark Overview 1. **Benchmark Name**: *Map vs Object with 100 keys and mutate* 2. **Description**: The benchmark evaluates the lookup and mutation performance of a `Map` and an `Object`. 3. **Setup**: A `Map` and an `Object` are initialized, and 100 key-value pairs are added using random values generated by `Math.random()`. ### Test Cases 1. **Map set**: - **Code**: `for (i = 0; i < count; i++) { map.set(getRandomInt(nbKeys), getRandomInt(42)); }` - This test case performs a mutation operation by setting a random key in the `Map` to a random value 1000 times. 2. **Obj set**: - **Code**: `for (i = 0; i < count; i++) { obj[getRandomInt(nbKeys)] = getRandomInt(42); }` - This test case performs a similar mutation operation on the plain JavaScript `Object` using the same method. ### Results Comparison - The benchmark results indicate that setting a value in an `Object` yields 5268.33 operations per second, while setting a value in a `Map` yields 4958.65 operations per second. This highlights that the `Object` performs better for this specific mutation operation. ### Pros and Cons **Map Pros**: - **Key Types**: Maps can have keys of any type (objects, functions, etc.), while an Object's keys are always strings (or symbols). - **Order**: Maps maintain the insertion order of keys, which can be significant for some algorithms. **Map Cons**: - **Performance**: As indicated by the benchmark, certain operations (like the ones tested) can be slower than using an Object, particularly when the keys are simple strings or integers. - **Memory**: Maps generally consume more memory due to additional overhead to handle intricate key types and ordering. **Object Pros**: - **Simplicity**: Objects are straightforward to use and have been present in JavaScript for a long time, making them familiar to most developers. - **Performance**: For many use cases, particularly those using string or integer keys, Objects can be faster due to their simpler design. **Object Cons**: - **Key Restrictions**: Limited to strings (or symbols) as keys, making them less versatile compared to Maps. - **Order**: Earlier versions of JavaScript did not guarantee order for Object keys (though the ECMAScript specification has since clarified this). ### Other Considerations - **Use Cases**: Choose `Map` when needing dynamic key types or insertion order. In contrast, use `Object` for simple, key-value stores where keys are guaranteed to be strings. - **Alternatives**: Other data structures include Sets (for collections of unique values) and other libraries like Immutable.js for persistent data structures if immutability is required. By understanding how `Map` and `Object` perform in specific scenarios, software engineers can select the appropriate data structure based on their application needs, balancing performance with versatility.
Related benchmarks:
Map vs Object (large number of keys)
Test map size
Test map size 2
Large Map vs Object 2
Map vs Object 5000 rand
Object vs Map 2022 new
Map vs Object with 100 keys
Map vs Object with 100 keys and 1/2 delete
Map vs Object with 100 keys and 1/5 delete on new item
Comments
Confirm delete:
Do you really want to delete benchmark?