Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object 10k iterations
(version: 0)
Lookup of map vs object
Comparing performance of:
Map lookup vs Obj lookup
Created:
4 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 = 10000, 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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's break down the benchmark definition and results. **What is being tested?** The benchmark compares the performance of two data structures in JavaScript: `Map` vs `Object`. Specifically, it measures how fast it is to look up a value by key in both cases. The test case uses 10,000 iterations for each lookup operation. **What options are compared?** Two options are being compared: 1. **Map**: A JavaScript `Map` object is used as the data structure. 2. **Object**: A regular JavaScript object (i.e., a "hash table") is used instead of a `Map`. **Pros and cons of each approach:** * **Map**: + Pros: More efficient for large datasets, as it uses a hash table internally, which allows for fast lookups. + Cons: May have higher memory overhead due to the internal hash table structure. * **Object** (regular JavaScript object): + Pros: Lower memory overhead compared to `Map`. + Cons: Less efficient for large datasets, as the lookup time increases with the size of the object. **Other considerations:** * The benchmark script prepares a `Map` and an empty object (`obj`) with one key-value pair each. * The test cases iterate 10,000 times, looking up the value associated with key `'a'` in both data structures. * The results show that the "Obj lookup" (i.e., using an object) has a slightly higher execution rate than the "Map lookup" (i.e., using a `Map`) on Chrome 98. **Library used:** None. This benchmark uses only built-in JavaScript features, such as `Map` and objects. **Special JS feature or syntax:** None are used in this benchmark. The code is straightforward and utilizes standard JavaScript features. **Other alternatives:** While not explicitly mentioned, some alternative approaches to using a `Map` or object for lookups could be: * Using an indexed array (e.g., `Array.prototype.find()` or `Array.prototype.findIndex()`) for smaller datasets. * Implementing a custom data structure with a more efficient lookup algorithm. * Utilizing third-party libraries like Lodash's `_.find()` or underscore.js's `_.indexOf()`, which provide optimized implementations of various array and object operations. These alternatives may have different trade-offs in terms of performance, memory usage, and ease of implementation.
Related benchmarks:
iterating from a filled object VS iterating from a map
Array from() vs Map.keys()
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?