Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object vs Array @adelinolsn v1
(version: 2)
Comparing performance of:
Array.find vs Map.get vs Object vs Map create vs Object create
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var quantityOfElements = 1000000; var array = new Array(quantityOfElements); var quantityOfTests = 10000; for (let i = 0; i < quantityOfElements; i++) { array[i] = { id: i }; }
Tests:
Array.find
for (let i = 0; i < quantityOfTests; i++) { const random = Math.floor(Math.random() * (quantityOfElements - 1)); array.find(a => a.id == random); }
Map.get
let map = new Map(); for (let i = 0; i < quantityOfElements; i++) { map.set(array[i].id, array[i]); } for (let i = 0; i < quantityOfTests; i++) { const random = Math.floor(Math.random() * (quantityOfElements - 1)); map.get(random); }
Object
let obj = {}; for (let i = 0; i < quantityOfElements; i++) { obj[array[i].id] = array[i]; } for (let i = 0; i < quantityOfTests; i++) { const random = Math.floor(Math.random() * (quantityOfElements - 1)); obj[random]; }
Map create
let map = new Map(); for (let i = 0; i < quantityOfElements; i++) { map.set(array[i].id, array[i]); }
Object create
let obj = {}; for (let i = 0; i < quantityOfElements; i++) { obj[array[i].id] = array[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Array.find
Map.get
Object
Map create
Object create
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.find
0.1 Ops/sec
Map.get
3.4 Ops/sec
Object
5.9 Ops/sec
Map create
3.6 Ops/sec
Object create
5.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark test case created on MeasureThat.net. The test compares the performance of three different data structures in JavaScript: arrays, objects, and maps. **Data Structures Compared** 1. **Array**: An array is a collection of values that can be accessed by index. 2. **Object**: An object is an unordered collection of key-value pairs, where each key is unique and corresponds to a specific value. 3. **Map**: A map (also known as an associative array) is an unordered collection of key-value pairs, where each key is unique and corresponds to a specific value. **Options Compared** The test compares the performance of different approaches for accessing elements in these data structures: * For arrays: `array.find()` (using the `find()` method) * For objects: direct property access (`obj[random]`) * For maps: + Creating a map (`map.set(array[i].id, array[i]);`) and then accessing an element using `map.get(random)` + Direct property access (`map[random]`) **Pros and Cons of Each Approach** 1. **Array.find()**: + Pros: concise and expressive code * Cons: may not be optimized for performance in all browsers or versions 2. **Direct Property Access (Object)**: + Pros: simple and fast * Cons: assumes the existence of a property with the desired value, which may lead to errors if the property does not exist 3. **Map.set() and map.get()**: + Pros: provides a way to store and retrieve data efficiently using a key-value pair approach * Cons: requires creating an empty map before populating it, which may add overhead 4. **Direct Property Access (Map)**: + Pros: similar to direct property access for objects, but with the added benefit of unique keys * Cons: assumes the existence of a key that matches the desired value, which may lead to errors if the key does not exist **Library and Special JS Features** The test uses the following library: * None explicitly mentioned, but it is likely that MeasureThat.net provides some internal libraries or utilities for benchmarking. There are no special JavaScript features mentioned in this test case.
Related benchmarks:
map vs forEach Chris
map vs forEach Chris v2b
Map.forEach vs Array.forEach vs Array.from(Map.prototype.values()).forEach
new Map vs set array to map
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?