Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map vs object wqe21123
(version: 1)
Comparing performance of:
object vs map
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
object
// Create sample data var array = {};
map
// Create sample data var array = new Map();
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:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
object
178386288.0 Ops/sec
map
55320584.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark in question compares two different data structures in JavaScript: a plain object (`{}`) and a `Map` object. Here’s a breakdown of what is tested and the considerations around it. ### Tested Options 1. **Object** - **Test Name:** `object` - **Benchmark Definition:** `var array = {};` (initializing a plain object) - **Notes:** Plain objects in JavaScript are key-value pairs that allow the storage of unordered collections. 2. **Map** - **Test Name:** `map` - **Benchmark Definition:** `var array = new Map();` (initializing a Map) - **Notes:** `Map` is a built-in JavaScript object that holds key-value pairs where keys can be any datatype. Maps maintain the insertion order of keys. ### Pros and Cons #### Objects **Pros:** - Lightweight and faster for simple use cases due to lower overhead. - Syntactic simplicity: object literals (`{}`) are easy to use and integral to JavaScript. **Cons:** - The keys in objects are limited to strings or symbols. If a non-string is used as a key, it will be automatically converted to a string. - Object properties can have prototype properties/methods which may lead to unintentional key collisions. #### Maps **Pros:** - Keys can be of any type, including objects, functions, and primitives. - Maps are optimized for frequent additions and removals of key-value pairs, making them more performant in dynamic scenarios. - They maintain insertion order, which can be crucial for certain applications. **Cons:** - Slightly more overhead in terms of memory due to additional functionality and flexibility. - Syntax for instantiation and operations is more verbose (e.g., `new Map()` vs. `{}`). ### Benchmark Results The benchmark results demonstrate the performance of both structures, measured in `ExecutionsPerSecond`. Here are the results: - **Object:** 178,386,288 executions per second - **Map:** 55,320,584 executions per second From the results, we can see that the plain object outperformed the Map by a significant margin, which is expected given that objects typically have less overhead. ### Other Alternatives Apart from objects and Maps, there are other data structures in JavaScript that can be used, depending on the specific requirements: 1. **Set**: If you only need to store unique values and do not care about keys or associated values, a Set is a good option. 2. **WeakMap**: Similar to Map, but the keys are weakly referenced. They can be garbage collected if there are no other references to them. Used for object caches without preventing garbage collection. 3. **WeakSet**: A Set that holds weakly referenced objects which can also be garbage collected. Each data structure has its specific use-case and understanding these differences helps in choosing the right one based on performance and functionality needs.
Related benchmarks:
Map vs Object
Object access by key vs Map.get
10k: Test Object.values vs array map for creating object array list v2
Array vs Object vs Map vs WeakMap access2
Array vs Object vs Map vs WeakMap access (1)
Array vs Object vs Map vs WeakMap access 22
Array vs Object vs Map vs WeakMap access (3)
Map.set vs Object assign
Array vs Object vs Map vs WeakMap access (3) vs Object key
Creating object (Map vs Object literal)
Comments
Confirm delete:
Do you really want to delete benchmark?