Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map vs obj vs array 2
(version: 0)
Comparing performance of:
map vs array vs obj
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var i = 0, count = 100000, a; var map = new Map(); var array = []; var obj = {}; for (i = 0; i < count; i++) { const randomValue = ~~(Math.random() * 100); map.set(i, randomValue); array.push({id: i, randomValue}); obj[i] = randomValue; }
Tests:
map
for (i = 0; i < count; i++) { a = map.get(i); }
array
for (i = 0; i < count; i++) { a = array.find(el => el.id = i); }
obj
for (i = 0; i < count; i++) { a = obj[i] }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
map
array
obj
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 is tested, the options compared, pros and cons of each approach, and other considerations. **Benchmark Definition** The benchmark measures the performance of three data structures in JavaScript: 1. `Map` (a built-in JavaScript object) 2. `Array` (a built-in JavaScript array) 3. A plain object (`obj`) created manually Each test case uses a different approach to access a value in each data structure. **Options Compared** The benchmark compares the performance of three options for accessing values in each data structure: 1. **Map**: Using the `get()` method 2. **Array**: Using the `find()` method or direct indexing (e.g., `array[i]`) 3. **Object**: Directly accessing a property using bracket notation (e.g., `obj[i]`) **Pros and Cons** Here are some pros and cons of each approach: * **Map**: + Pros: Fast lookups, efficient iteration. + Cons: Not all browsers support `get()` method; requires JavaScript engine support. * **Array**: + Pros: Wide browser support, simple implementation. + Cons: Slower lookups compared to Map or Object, especially for large arrays. * **Object**: + Pros: Fast and efficient direct access, no additional dependencies required. + Cons: Not suitable for large datasets; requires manual property creation. **Other Considerations** * **Library Usage**: None in this benchmark. No libraries are used in the test cases. * **Special JS Features or Syntax**: Some browsers (e.g., Chrome Mobile iOS 111) support newer JavaScript features, but it's not relevant to this specific benchmark. * **Device Platform and OS**: The benchmark is run on a mobile device with iOS 16.3, which might affect performance due to the mobile environment. **Alternatives** If you were to rewrite this benchmark, you could consider alternative data structures or access methods, such as: * Using `WeakMap` instead of `Map` * Implementing custom iterators for each data structure * Using a third-party library like Lodash to compare performance between different array and object access methods Keep in mind that the choice of data structure and access method depends on the specific use case and requirements.
Related benchmarks:
Map vs Array vs Object set uint32 key speed
Map vs Array vs Object has uint32 key speed
map vs obj vs array
Array vs Class
Comments
Confirm delete:
Do you really want to delete benchmark?