Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object vs map 100
(version: 0)
Comparing performance of:
Create object vs Create map vs Update obj vs Update map vs Obj size vs Map size vs Map obj vs Map map
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var items = Array.from({ length: 20000 }, (_, i) => ({ id: `item-${i}` })) var dataObj = {}; items.forEach((item, i) => { dataObj[item.id] = { start: i * 30, end: i * 30 + 30 } }); var dataMap = new Map(); items.forEach((item, i) => { dataMap.set(item.id, { start: i * 30, end: i * 30 + 30 }); });
Tests:
Create object
const obj = {}; items.forEach((item, i) => { if (item.id === "__proto__" || item.id === "constructor") return; obj[item.id] = { start: i * 30, end: i * 30 + 30 } });
Create map
const map = new Map(); items.forEach((item, i) => { map.set(item.id, { start: i * 30, end: i * 30 + 30 }); });
Update obj
items.forEach((item, i) => { dataObj[item.id] = { start: i * 40, end: i * 40 + 40 } }); const obj = { ...dataObj };
Update map
items.forEach((item, i) => { dataMap.set(item.id, { start: i * 40, end: i * 40 + 40 }); }); const map = new Map(dataMap);
Obj size
Object.keys(dataObj).length;
Map size
dataMap.size;
Map obj
Object.entries(dataObj).map(([key, data]) => { console.log(key, data); });
Map map
Array.from(dataMap).map(([key, data]) => { console.log(key, data); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (8)
Previous results
Fork
Test case name
Result
Create object
Create map
Update obj
Update map
Obj size
Map size
Map obj
Map map
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 benchmark and explain what's being tested. **Benchmark Overview** The benchmark is comparing different approaches to create, update, and access objects and maps in JavaScript. The goal is to measure which approach is faster and more efficient. **Script Preparation Code** The script preparation code creates two datasets: `dataObj` (an object) and `dataMap` (a map). Both datasets are populated with the same data, but using different approaches: * `dataObj`: The object is created by iterating over the `items` array and assigning each item to an existing property on the object. This approach uses the prototype chain. * `dataMap`: The map is created by iterating over the `items` array and setting each item as a key-value pair in the map. **Html Preparation Code** The html preparation code is empty, indicating that this benchmark does not rely on HTML rendering or DOM manipulation. **Test Cases** There are six test cases: 1. **Create object**: Measures the time it takes to create an object using the `dataObj` approach. 2. **Create map**: Measures the time it takes to create a map using the `dataMap` approach. 3. **Update obj**: Measures the time it takes to update an object by assigning a new value to an existing property on the object. This test case is similar to "Create object", but with an additional step: updating the existing data. 4. **Obj size**: Measures the time it takes to access the size of the `dataObj` object (i.e., its length). 5. **Update map**: Measures the time it takes to update a map by setting a new value for an existing key-value pair. 6. **Map map**: Measures the time it takes to access the contents of the `dataMap` map (i.e., iterate over its key-value pairs). **Execution Results** The execution results show the number of executions per second (in executions per second) for each test case, which indicates the average speed at which the benchmark was executed. **Observations and Recommendations** Based on the results, it appears that: * Creating an object using the prototype chain (`dataObj`) is slower than creating a map (`dataMap`). * Updating an existing object or map is significantly faster than creating a new one. * Accessing the size of an object (or iterating over its contents) can be slow if the data set is large. Recommendations: * Use maps instead of objects when working with large datasets, as they are more efficient for iteration and lookup. * Update existing data instead of creating new data to avoid unnecessary computations. * Optimize object access patterns by using techniques like caching or lazy loading.
Related benchmarks:
map vs forEach Chris v2b
Copy map vs. object
Copy map vs. object 10000
make map with entries vs for of
Comments
Confirm delete:
Do you really want to delete benchmark?