Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object vs Array @adelinolsn v1.2
(version: 0)
Comparing performance of:
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:
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 (2)
Previous results
Fork
Test case name
Result
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
Map create
3.2 Ops/sec
Object create
5.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **What is tested?** The benchmark compares the performance of two data structures in JavaScript: objects (specifically, using the syntax `obj[array[i].id] = array[i];`) and Maps (`map.set(array[i].id, array[i]);`). **Options compared** There are two options being compared: 1. **Objects**: The first test case uses an object literal to create a new object with properties set dynamically. 2. **Maps**: The second test case uses the `Map` constructor to create a new Map data structure. **Pros and cons of each approach:** * **Objects**: + Pros: Lightweight, easy to use, and widely supported by browsers. + Cons: Properties are accessed using dot notation (e.g., `obj.array[i].id`) which can lead to slower lookups due to property resolution. * **Maps**: + Pros: Faster lookups compared to objects, especially for large datasets, since Maps use a hash table internally. + Cons: May be less familiar or intuitive to use, and requires more code to set properties. **Library usage** None of the test cases use any external libraries. The `Map` constructor is a built-in JavaScript feature introduced in ECMAScript 2015 (ES6). **Special JS features or syntax** There are no special JavaScript features or syntaxes being used in these test cases. They are standard, everyday JavaScript code. **Other alternatives** If you wanted to compare the performance of objects and Maps, but with some variation in the data structure (e.g., using a nested object instead of an array), you could modify the benchmark to include additional test cases. For example: ```json { "Name": "Map vs Object vs Nested Object @adelinolsn v1.2", // ... rest of the JSON remains the same ... } ``` With this additional variation, you would add another test case that uses a nested object structure (e.g., `let obj = {}; for (let i = 0; i < quantityOfElements; i++) { obj[array[i].id] = { foo: bar }; }`). Keep in mind that using a nested object structure can lead to slower performance compared to simple objects or Maps, since it requires more computations to traverse the nested property chain.
Related benchmarks:
map vs forEach Chris
map vs forEach Chris v2b
Slice vs Map (jv)
new Map vs set array to map
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?