Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test Map vs Object
(version: 1)
Comparing performance of:
Map vs Obj
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var map = new Map(); var obj = {}; map.set('a', 5); obj['a'] = 5; var i = 0, count = 10000, a;
Tests:
Map
for (i = 0; i < count; i++) { a = map.get('a'); map.set('b',1); }
Obj
for (i = 0; i < count; i++) { a = obj['a']; obj['b'] = 1 }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map
Obj
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map
16036.6 Ops/sec
Obj
220316.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON is a comparison between two JavaScript data structures: `Map` and plain JavaScript `Object`. The goal is to evaluate their performance, particularly in read and write operations. ### Comparison of Options 1. **Map** - **Construction**: `var map = new Map();` - **Benchmark Operations**: - Retrieving a value: `a = map.get('a');` - Setting a new value: `map.set('b', 1);` - **Advantages**: - `Map` maintains the insertion order of its elements. - It allows for any data type as keys, including objects and functions. - Typically provides better performance for frequently changing collections, particularly when the size is large. - **Disadvantages**: - Slightly higher memory overhead compared to objects, particularly for small datasets. - Set-up time can be marginally longer since it is a dedicated data structure. 2. **Object** - **Construction**: `var obj = {};` - **Benchmark Operations**: - Retrieving a value: `a = obj['a'];` - Setting a new value: `obj['b'] = 1;` - **Advantages**: - Simpler and more lightweight for basic key-value storage. - Houses various methods and properties directly available on objects. - Widely familiar to JavaScript developers and often sufficient for many use cases. - **Disadvantages**: - Does not maintain insertion order for keys (prior to ES6, though ES6 does guarantee insertion order for strings, it can still be less intuitive). - Only strings and symbols can be used as keys (a limitation when needing non-string keys). ### Benchmark Execution The benchmark consists of running the defined operations in a loop (10,000 iterations) for both `Map` and `Object`. 1. **Map Execution Stats**: - **Executions Per Second**: 1114.79 (approx.) - **Test Name**: "Map" 2. **Object Execution Stats**: - **Executions Per Second**: 1201.59 (approx.) - **Test Name**: "Obj" From these results, it’s observable that the `Object` implementation is slightly faster (1201.59 EPS) than the `Map` implementation (1114.79 EPS). This can be attributed to the added overhead that `Map` has in terms of its complexity and features compared to plain objects. ### Other Considerations - **Use Cases**: The choice between `Map` and `Object` often depends on use case needs. If you require non-string keys, complex keys, or need to maintain order, prefer `Map`. Use `Object` for simpler key-value pairs or when leveraging prototype inheritance. - **Alternatives**: If the performance benchmarks matter in high-performance situations, consider: - **WeakMap**: Useful for maintaining associations without preventing garbage collection when keys are no longer accessible. - **Arrays**: Depending on the use case, arrays can sometimes replace maps or objects, particularly for ordered lists of values. In summary, the benchmark clearly highlights the performance differences between `Map` and `Object`, guiding developers in choosing the appropriate data structure based on their specific requirements while also outlining the pros and cons of each approach.
Related benchmarks:
Map vs Object
Map vs Object 2
Obj vs Map
Map vs Object2
Map vs Object addx
Map vs Object (w/delete)
Map vs Object (large)
Map Find Object
Map vs Object Creation
Comments
Confirm delete:
Do you really want to delete benchmark?