Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object: Lookup, insert and delete
(version: 0)
Comparing performance of:
Map operations vs Obj operations
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var map = new Map(); var obj = {}; var i = 0, count = 1000, a; function getOnMap(uid) { if (map.has(uid)) { return map.get(uid); } let storage = {}; map.set(uid, storage); return storage; } function getOnObj(uid) { let storage = obj[uid]; if (!storage) { storage = obj[uid] = {}; } return storage; }
Tests:
Map operations
for (i = 0; i < count; i++) { a = getOnMap(i); map.delete(i); }
Obj operations
for (i = 0; i < count; i++) { a = getOnObj(i); delete obj[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map operations
Obj operations
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Overview** The benchmark compares two approaches: using a `Map` data structure in JavaScript and using an object (`{}`) to store data. Both approaches have two types of operations: 1. **Lookup**: Retrieving a value by its key. 2. **Insertion**: Adding a new key-value pair to the data structure. 3. **Deletion**: Removing a key-value pair from the data structure. **Comparison** The benchmark defines two test cases: 1. `Map operations`: It performs 1000 iterations of inserting, looking up, and deleting entries in the `Map`. 2. `Obj operations`: It performs 1000 iterations of inserting, looking up, and deleting entries in an object. **Options compared** The options being compared are: * Using a `Map` (a built-in JavaScript data structure) versus using an object (`{}`). * The two approaches have different characteristics: + **Cache behavior**: `Maps` store references to the values, while objects store the actual values. This means that when you delete an entry from an object, it doesn't immediately affect the stored reference. + **Insertion overhead**: Creating a new property in an object can be slower than setting a value on a map. **Pros and Cons** * **Maps**: + Pros: Efficient lookups, insertions, and deletions, especially for large datasets. They also provide good cache behavior. + Cons: May have higher memory overhead due to storing references to values. * **Objects**: + Pros: Lightweight, easy to use, and suitable for small to medium-sized datasets. Object properties can be faster to set than map entries. + Cons: Poor cache behavior (deletion doesn't immediately affect stored references), and insertion can be slower. **Library usage** There is no explicit library mentioned in the benchmark definition or test cases. However, `Map` is a built-in JavaScript data structure, so no additional libraries are needed. **Special JS features** This benchmark does not use any special JavaScript features like `async/await`, `Promises`, or `Generators`. It's focused on basic operations and characteristics of the two data structures being compared. **Alternatives** Other alternatives for comparing these two approaches could include: * Using a different data structure, such as an array or a custom implementation. * Adding additional operations (e.g., updating values, iterating over keys). * Comparing performance with other programming languages or frameworks.
Related benchmarks:
Map vs object for deletions
Array from() vs Map.keys()
Object spread vs New map with string keys
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?