Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
[aech0Xod] Map vs Object
(version: 0)
map vs object
Comparing performance of:
Map lookup vs Obj lookup vs Map delete vs Obj delete
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 delete
for (i = 0; i < count; i++) { map.delete('a'); }
Obj delete
for (i = 0; i < count; i++) { delete obj['a']; }
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 delete
Obj delete
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):
I'll break down the benchmark and explain what's being tested, compared options, pros/cons, and other considerations. **Benchmark Overview** The test is comparing the performance of two data structures in JavaScript: `Map` and objects (`{}`). The test involves creating a map and an object, setting key-value pairs, and then performing lookups and deletes on both data structures. The goal is to determine which one performs better. **Comparison Options** There are four tests: 1. **Map lookup**: Iterating over the map using `map.get()` to retrieve a value. 2. **Object lookup**: Iterating over an object using bracket notation (`obj['a']`) to retrieve a value. 3. **Map delete**: Iterating over the map using `map.delete()` to remove a key-value pair. 4. **Object delete**: Iterating over an object using `delete` keyword to remove a property. **Pros and Cons** 1. **Map**: * Pros: Map is designed for fast lookups, provides iterators, and supports hash collisions. * Cons: May have slower initial creation time compared to objects, and may not be as widely supported in older browsers. 2. **Objects (bracket notation)**: * Pros: Widely supported across all browsers, objects are lightweight, and bracket notation is simple. * Cons: Bracket notation can lead to slower lookups, especially for large datasets. **Other Considerations** 1. **Library usage**: Neither `Map` nor object properties use any external libraries. The performance difference between the two is directly related to the underlying implementation of these data structures in JavaScript engines. 2. **Special JS features**: There are no special JavaScript features or syntax used in this benchmark, making it accessible to a wide range of developers. **Alternative Approaches** Other approaches that could be used for similar benchmarks include: 1. Using `Array.prototype.forEach()` and accessing elements by index (e.g., `arr[i]`) instead of using bracket notation. 2. Creating an array of objects or a map-like object (using `Object.create()`, for example) to compare performance with the standard `Map` and objects approach. 3. Utilizing more advanced data structures like sets, queues, or trees to create additional benchmarks. These alternatives can help developers explore different aspects of JavaScript's built-in data structures and optimize their code for specific use cases.
Related benchmarks:
_.map over object vs Object.values.map
Array from() vs Map.keys()
Array from() vs Map.keys() vs Map.values() vs spread
Array from() vs Map.keys() vs Map.values() vs spread (fixed)
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?