Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object with 100 keys and 1/5 delete on new item
(version: 1)
Lookup of map vs object
Comparing performance of:
Map set vs Obj set
Created:
7 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
const nbKeys = 100; var i = 0, count = 1000, a; function getRandomInt(max) { return Math.floor(Math.random() * max); } const deleteFreq = 5;
Tests:
Map set
for( let nbObj = 0; nbObj < 25; nbObj++) { let localmap = new Map(); for (i = 0; i < count; i++) { const deleteOrSet = getRandomInt(deleteFreq) !== 0 if(deleteOrSet) localmap.delete(getRandomInt(nbKeys)); else localmap.set(getRandomInt(nbKeys), getRandomInt(42)); } }
Obj set
for( let nbObj = 0; nbObj < 25; nbObj++) { let localobj = {}; for (i = 0; i < count; i++) { const deleteOrSet = getRandomInt(deleteFreq) !== 0 if(deleteOrSet) delete localobj[getRandomInt(nbKeys)]; else localobj[getRandomInt(nbKeys)] = getRandomInt(42); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map set
Obj set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 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 set
151.9 Ops/sec
Obj set
154.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 7 months ago):
In this benchmark, the performance of two data structures in JavaScript, `Map` and `Object`, is tested for their efficiency in handling operations of adding and deleting properties (or keys). ### Benchmark Overview **Benchmark Name:** Map vs Object with 100 keys and 1/5 delete on new item **Description:** The benchmark compares the lookup performance of a `Map` versus an `Object`. ### Test Case Explanation #### Test Cases 1. **Test Case: Map set** ```javascript for ( let nbObj = 0; nbObj < 25; nbObj++) { let localmap = new Map(); for (i = 0; i < count; i++) { const deleteOrSet = getRandomInt(deleteFreq) !== 0; if (deleteOrSet) localmap.delete(getRandomInt(nbKeys)); else localmap.set(getRandomInt(nbKeys), getRandomInt(42)); } } ``` - **Description:** This test initializes a `Map` and performs a specified number of set and delete operations. The operations are randomized such that on average 1 in every 5 operations is a delete action. 2. **Test Case: Obj set** ```javascript for ( let nbObj = 0; nbObj < 25; nbObj++) { let localobj = {}; for (i = 0; i < count; i++) { const deleteOrSet = getRandomInt(deleteFreq) !== 0; if (deleteOrSet) delete localobj[getRandomInt(nbKeys)]; else localobj[getRandomInt(nbKeys)] = getRandomInt(42); } } ``` - **Description:** This test works similarly to the previous one but uses a plain JavaScript object. It creates an object and performs the same mixed operations of adding and deleting keys. ### Options Compared 1. **JavaScript `Map`:** - **Pros:** - Maintains the order of entries. - Can use objects and functions as keys. - Generally performs better for frequent additions and deletions. - **Cons:** - Slightly larger memory footprint compared to simple objects. - Might have slightly less performance for operations where the key set is static and not frequently modified. 2. **JavaScript `Object`:** - **Pros:** - Simpler and more lightweight implementation. - Suitable for scenarios where you only need string keys and have no concern for key order. - **Cons:** - Less efficient at handling large numbers of dynamic additions and deletions compared to `Map`. - Does not maintain key order, which can lead to unexpected behaviors in certain scenarios. ### Considerations - **Frequency of Operations:** If your application requires frequent additions and deletions, `Map` is generally preferred for better performance. - **Key Types:** If your keys will be strings only, an `Object` may suffice. If you require objects as keys, a `Map` will be necessary. - **Memory Overhead:** While managing memory, consider that `Map` may consume more memory due to its flexibility. ### Alternative Approaches - If neither a `Map` nor an `Object` meets the requirements, other alternatives like `WeakMap` (for key references without preventing garbage collection) or using a third-party library such as Immutable.js or Lodash's collection methods could be considered. - For storing large datasets with complex relationships, other data structures like arrays or hashes from libraries specifically designed for performance may be more suitable. ### Conclusion This benchmark serves to highlight the performance differences between a `Map` and an `Object` in typical use cases. While both structures have their place in JavaScript development, understanding their strengths and weaknesses enables developers to make informed choices based on application needs.
Related benchmarks:
Test map size
Test map size 2
Map vs Object 5000 rand
Map vs Object with random keys
Map get VS Map has get part 2
Map vs Object (real-world) Performance (better)
Map vs Object with 100 keys
Map vs Object with 100 keys and mutate
Map vs Object with 100 keys and 1/2 delete
Comments
Confirm delete:
Do you really want to delete benchmark?