Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map() vs Object() + Reduce vs Iteration
(version: 0)
Comparing performance of:
Map vs Object Iteration
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { 'a': { id: 'a', num: 1 }, 'b': { id: 'b', num: 1 }, 'c': { id: 'c', num: 1 }, 'd': { id: 'd', num: 1 }, 'e': { id: 'e', num: 1 }, 'f': { id: 'f', num: 1 }, 'g': { id: 'g', num: 1 }, }; var objmap = new Map(Object.entries(obj)) function callback(acc, value) { return acc + value.num }
Tests:
Map
for (var i=10000; i > 0; i--) { Array.from(objmap.values()).reduce(callback, 0); }
Object Iteration
for (var i=10000; i > 0; i--) { let index = 0; let result = 0; for (const id in obj) { const value = obj[id]; result = callback(result, value, index); index += 1; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map
Object Iteration
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 17 on iOS 17.2
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map
661.3 Ops/sec
Object Iteration
245.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and discussed. **Benchmark Definition** The benchmark is designed to compare the performance of three approaches: 1. `Map()`: Using JavaScript's built-in `Map` data structure to store and iterate over the object's values. 2. Object Iteration: Iterating directly over the object's properties using a traditional `for...in` loop. 3. Reduce vs Iteration (this is a nested option, but we'll discuss it separately): This option tests both object iteration and map reduce. **Script Preparation Code** The script prepares an object `obj` with 7 properties, each containing an `id` and a `num` value. A new `Map` instance is created from this object's entries using `Object.entries(obj)`. Additionally, a callback function `callback` is defined to accumulate the sum of all `num` values in the map. **Html Preparation Code** There is no HTML preparation code provided for this benchmark, which suggests that the benchmark is designed to run solely on the JavaScript engine. **Individual Test Cases** Two test cases are defined: 1. **Map**: This test case iterates over the map's values using the `Array.from()` method and reduces the sum of all `num` values. 2. **Object Iteration**: This test case iterates directly over the object's properties using a traditional `for...in` loop, calculating the same sum as in the Map test case. **Other Alternatives** The benchmark does not explicitly mention other alternatives, but we can consider additional approaches: * Using `Array.prototype.forEach()` instead of `Array.from()`. * Utilizing modern JavaScript features like `forEach()` or `reduce()` on the object's entries. * Implementing custom iteration logic using a different data structure (e.g., an array). **Libraries and Features** There is no explicit library mentioned in the benchmark definition. However, the use of `Map` and its methods suggests that the JavaScript engine supports this feature. The callback function `callback` is used to accumulate the sum of all `num` values. This implies the use of functional programming concepts. **Device Platform and Browser** The latest benchmark result shows the test results for a Mobile Safari 17 browser running on an iPhone with iOS 17.2 operating system. In summary, this benchmark compares the performance of three approaches: using JavaScript's built-in `Map` data structure, iterating directly over an object's properties, and reducing the sum of all values in both cases. The test results provide insight into the performance characteristics of these approaches on a specific device platform and browser.
Related benchmarks:
Map & Object.fromEntries vs reduce
Object.fromEntries vs reduce v3
flatMap vs reduce test 2
Flatmap vs reduce with objects
Object.fromEntries(Array.map) vs Array.reduce (with different methods)
Comments
Confirm delete:
Do you really want to delete benchmark?