Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object
(version: 0)
Lookup of map vs object
Comparing performance of:
Map lookup vs Obj lookup
Created:
9 years ago
by:
Registered User
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 = 1000, a;
Tests:
Map lookup
for (i = 0; i < count; i++) { a = map.get('a'); }
Obj lookup
for (i = 0; i < count; i++) { a = obj['a']; }
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:
3 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map lookup
10197.0 Ops/sec
Obj lookup
10392.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and the pros/cons of each approach. **Benchmark Definition: Map vs Object** The benchmark measures the performance difference between looking up values in a `Map` object versus an `Object`. A `Map` is a data structure that stores key-value pairs, whereas an `Object` is a general-purpose data type in JavaScript that can store any number of key-value pairs. **Script Preparation Code** ```javascript var map = new Map(); var obj = {}; map.set('a', 5); obj['a'] = 5; ``` This code creates two objects: `map` and `obj`. It then sets a value for the key `'a'` in both objects using the `set()` method for `Map` and the bracket notation `[]` for `Object`. **Html Preparation Code** The `Html Preparation Code` is empty, which means that this benchmark does not test any HTML-related performance. **Individual Test Cases: Map Lookup and Object Lookup** There are two test cases: 1. **Map Lookup**: This test case measures the time it takes to iterate 1000 times over the map and look up the value for the key `'a'` using the `get()` method. 2. **Object Lookup**: This test case measures the time it takes to iterate 1000 times over the object and look up the value for the key `'a'` using the bracket notation `[]`. **Library Used: None** Neither of these test cases uses any external libraries. **Special JS Feature/Syntax: None** There are no special JavaScript features or syntax used in this benchmark. **Pros and Cons:** * **Map Lookup**: Using a `Map` object can be beneficial if you need to look up values by key multiple times, as it provides faster lookup times compared to `Object`. However, creating a `Map` object requires additional memory and may not be suitable for all use cases. * **Object Lookup**: Using an `Object` is more straightforward and widely supported. It can also be useful if you need to store arbitrary data in the object. **Other Alternatives:** If you needed to compare other data structures, such as: * Arrays vs Objects * Sets vs Arrays * Maps vs Sets You could create similar benchmarks using different script preparation codes and test cases. For example, to benchmark arrays vs objects, you could use: ```javascript var arr = []; var obj = {}; for (i = 0; i < count; i++) { arr.push(i); obj[i] = i; } ``` This would measure the time it takes to iterate over an array and add elements versus iterating over an object and setting properties. Similarly, you could benchmark sets vs arrays or maps vs sets using different script preparation codes and test cases.
Related benchmarks:
Array from() vs Map.keys()
Array from() vs Map.keys() vs Map.values() vs spread
array includes vs object key lookup, large arrays
Map vs Object read performance for a 1000 key lookup
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?