Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object - Lookup and write
(version: 0)
Lookup/write of map vs object
Comparing performance of:
Map lookup vs Obj lookup vs Map set vs Obj set
Created:
3 years 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 = 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']; }
Map set
for (i = 0; i < count; i++) { a = map.set(i+'', i); }
Obj set
for (i = 0; i < count; i++) { obj[i+''] = i; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Map lookup
Obj lookup
Map set
Obj set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 17_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/129.0.6668.69 Mobile/15E148 Safari/604.1
Browser/OS:
Chrome Mobile iOS 129 on iOS 17.6
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map lookup
12946.3 Ops/sec
Obj lookup
12538.8 Ops/sec
Map set
8691.7 Ops/sec
Obj set
11413.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **What is being tested?** The benchmark compares the performance of two data structures in JavaScript: Maps and objects (specifically, regular objects). The tests focus on three main operations: 1. Lookup 2. Write (set) These operations are common in many applications, such as caching, configuration management, or storing key-value pairs. **Options being compared** The benchmark is testing the following options: * **Map lookup**: `map.get('a')` * **Object lookup**: `obj['a']` * **Map set**: `map.set(i+'', i)` * **Object set**: `obj[i+''] = i` These tests will reveal how efficient each data structure is for these common operations. **Pros and cons of each approach** Here's a brief summary: * **Map lookup**: Maps are optimized for fast lookups, as they use a hash table to store keys and values. This makes map lookups typically faster than object lookups. + Pros: Fast lookups, good cache locality + Cons: More memory-intensive due to the overhead of the hash table * **Object lookup**: Objects are more flexible but slower for lookups because they rely on property name-based indexing. + Pros: Flexible data structure, easy to implement + Cons: Slower lookups compared to maps * **Map set**: Map sets involve both a lookup and a write operation, which can be inefficient if the set is large. However, this test aims to evaluate the overhead of setting values in a map. + Pros: Can be efficient for inserting or updating multiple values at once + Cons: Can be slower than individual lookups and writes due to the overhead of inserting values * **Object set**: Object sets involve both an insert and a write operation, similar to map sets. This test aims to evaluate the efficiency of setting values in a regular object. + Pros: Similar benefits as map sets for large number of inserts + Cons: May be slower due to the overhead of property name-based indexing **Other considerations** * **Memory usage**: Maps typically require more memory than objects due to the hash table used for storing keys and values. This can impact performance in terms of available resources. * **Cache locality**: Maps are designed to reduce cache misses, which means they tend to have better cache locality compared to objects. **Alternative approaches** There are other data structures that could be tested in this benchmark, such as: * Sets: Similar to maps but only allow unique values and do not support lookups or writes. * Arrays: While arrays can be used for storing key-value pairs, they may not offer the same performance benefits as maps or objects due to the overhead of indexing. * Linked lists: A data structure where each node points to the next node in the list. This would likely result in slower performance compared to the other options. Keep in mind that this benchmark is focused on JavaScript-specific features and optimizations, so other programming languages might have different results or approaches for similar tests.
Related benchmarks:
Map vs Object single op
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
Comments
Confirm delete:
Do you really want to delete benchmark?