Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object with 100 keys
(version: 1)
Lookup of map vs object
Comparing performance of:
Map lookup vs Obj lookup
Created:
8 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 lookup
for (i = 0; i < count; i++) { a += map.get(getRandomInt(nbKeys)); }
Obj lookup
for (i = 0; i < count; i++) { a = obj[getRandomInt(nbKeys)]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map lookup
Obj lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 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 lookup
7757.8 Ops/sec
Obj lookup
8772.6 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 8 months ago):
In this benchmark test, the performance of two data structures in JavaScript—`Map` and `Object`—is compared specifically in terms of their lookup speed. The benchmark defines two main test cases: one for looking up values in a `Map` and another for looking up values in a plain JavaScript `Object`. ### Benchmark Overview 1. **Tested Data Structures**: - **Map**: A built-in JavaScript object that stores key-value pairs where keys can be any data type, offering ordered iterations. - **Object**: A basic data structure that stores key-value pairs, with keys being strings or symbols. 2. **Preparation Code**: - The preparation code initializes a `Map` and an `Object`, populating both with 100 random key-value pairs. The keys are integers from `0` to `99`, and the values are generated using `Math.random()`. 3. **Test Cases**: - **Map Lookup**: This test case retrieves values from the `Map` using a randomly generated key (from 0 to 99) in a loop executed 1000 times. The code retrieves performance metrics based on the number of executions per second. - **Object Lookup**: This test performs a similar operation but retrieves values from the `Object` in the same manner. ### Results - The results display the execution speed for each test case in executions per second: - **Object Lookup**: 8772.61 executions per second (faster). - **Map Lookup**: 7757.76 executions per second. ### Pros and Cons 1. **Map**: - **Pros**: - Maintains the order of keys. - Supports keys of any type (including objects). - More efficient when frequently adding/removing key-value pairs. - **Cons**: - Generally slower than `Object` for simple lookups with string keys. - Slightly more memory overhead. 2. **Object**: - **Pros**: - Typically faster for lookups of string keys due to optimizations. - Uses less memory in simple use cases. - **Cons**: - Key types are limited to strings and symbols. - Does not maintain order of keys in all JavaScript engines (prior to ES6). ### Other Considerations When choosing between `Map` and `Object`, consider the nature of the use case. If you need unordered keys or key types beyond strings, `Map` is preferable. However, for simple collections of properties (with string keys), traditional `Object` is often sufficient and generally faster. ### Alternatives Besides `Map` and `Object`, other alternatives for key-value storage in JavaScript include: - **WeakMap**: Similar to `Map`, but with key restrictions (keys must be objects) and automatic garbage collection of keys no longer in use. - **Set**: Stores unique values but does not maintain key-value pairs. - **WeakSet**: Similar to `Set`, but holds only weak references to objects, facilitating garbage collection. ### JavaScript Features and Syntax The benchmark uses standard JavaScript syntax and constructs such as: - **`for` Loop**: For iterating over a set number of operations. - **Arrow Functions**: Not utilized in this benchmark, but often used to create concise function expressions. - **Math.random()**: A built-in JavaScript function to generate random numbers. This benchmark provides valuable insights into the performance characteristics of different JavaScript data structures, enabling developers to make informed choices based on the needs of their applications.
Related benchmarks:
Map vs Object (large number of keys)
Test map size
Test map size 2
Map vs Object 5000 rand
Map vs Object (3)
Object vs Map 2022 new
Map vs Object with 100 keys and mutate
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?