Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce vs Map+FromEntries vs for loop vs forEach
(version: 0)
Comparing performance of:
reduce vs map + fromEntries vs for loop vs forEach
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = Array.from(Array(10000).keys());
Tests:
reduce
Object.entries(data).reduce((acc, [k, v]) => { acc[k] = v.toString(); return acc; }, {});
map + fromEntries
Object.fromEntries(Object.entries(data).map(([k, v]) => ([k, v.toString()])));
for loop
const dataObject = Object.entries(data) const acc = {} for (let i = 0; i < dataObject.length; i++) { const [k, v] = dataObject[i]; acc[k] = v.toString() }
forEach
const dataObject = Object.entries(data) const acc = {} dataObject.forEach(([k, v]) => {acc[k] = v.toString()})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
reduce
map + fromEntries
for loop
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/139.0.0.0 Safari/537.36
Browser/OS:
Chrome 139 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
1978.5 Ops/sec
map + fromEntries
1248.7 Ops/sec
for loop
2309.8 Ops/sec
forEach
2263.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark and explain what's being tested. The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The test compares four different approaches to iterate over an array of objects (in this case, `data`): reduce, map+fromEntries, for loop, and forEach. **Approaches:** 1. **Reduce**: This approach uses the `Array.prototype.reduce()` method to accumulate values in an object. It's a concise way to process arrays, but it can be slower than other approaches. 2. **Map + From Entries**: This approach uses `Array.prototype.map()` to create a new array of objects, and then passes the resulting array to `Object.fromEntries()` to convert it back into an object. While this approach is often used in modern JavaScript code, it's not always the most efficient way to process arrays. 3. **For Loop**: This approach uses a traditional for loop to iterate over the array of objects, accessing each element by its index and assigning values to the resulting object. This approach can be slower due to the overhead of indexing and assignment operations. 4. **ForEach**: This approach uses `Array.prototype.forEach()` to iterate over the array of objects, passing a callback function that assigns values to the resulting object. Like for loops, this approach incurs additional overhead due to callback function invocation. **Pros and Cons:** * Reduce: + Pros: concise, easy to read + Cons: can be slower than other approaches, may not be as efficient for large arrays * Map + From Entries: + Pros: often used in modern JavaScript code, creates a new object with the desired structure + Cons: can create unnecessary intermediate data structures and may be slower due to overhead of `Object.fromEntries()` * For Loop: + Pros: control over iteration order and indexing + Cons: can be verbose, incurs additional overhead due to indexing and assignment operations * ForEach: + Pros: concise, easy to read (in some cases) + Cons: incurs additional overhead due to callback function invocation **Library/Function Usage:** In this benchmark, `Array.prototype.reduce()`, `Array.prototype.map()`, `Object.fromEntries()`, and `Array.prototype.forEach()` are all standard JavaScript library functions. These functions provide a convenient way to process arrays and objects in JavaScript. **Special JS Features/Syntax:** This benchmark does not specifically test any special JavaScript features or syntax, such as async/await, arrow functions, or modules. **Other Alternatives:** If you were looking for alternative approaches to iterate over an array of objects, some other options could include: * Using `Array.prototype.forEach()` with a custom callback function that uses the `in` operator to access object properties. * Utilizing modern JavaScript features like `for...of` loops or `flatMap()`. * Employing third-party libraries or frameworks that provide optimized iteration utilities. Keep in mind that these alternatives may have varying performance characteristics, readability trade-offs, and compatibility with different browsers or environments.
Related benchmarks:
reduce (immutable) vs map + fromEntries
Object.fromEntries vs reduce vs Map + Array.from
Map & Object.fromEntries vs reduce
Object.fromEntries on array vs reduce on array
Object.fromEntries vs reduce vs Map vs foreach
Comments
Confirm delete:
Do you really want to delete benchmark?