Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs reduce vs for - fill object with object
(version: 0)
Comparing performance of:
forEach vs reduce vs for
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 50000; i++) { arr.push(i); }
Tests:
forEach
var objForEach = {}; arr.forEach(item => { objForEach[item] = { exists: true }; });
reduce
var objReduce = arr.reduce((acc, item) => { acc[item] = { exists: true }; return acc; }, {});
for
var objFor = {}; for (var j = 0; j < arr.length; j++) { var item = arr[j]; objFor[item] = { exists: true }; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
forEach
reduce
for
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.1:latest
, generated one year ago):
Let's dive into the benchmark. **What is being tested?** The benchmark is testing three different ways to fill an object (`objForEach`, `objReduce`, and `objFor`) with data from an array (`arr`) of 50,000 items. The goal is to compare the performance (speed) of these three approaches: using `forEach`, `reduce`, and a traditional `for` loop. **What options are compared?** The benchmark compares three options: 1. **ForEach**: Using the `forEach` method on the array to iterate over its elements and add them to an object. 2. **Reduce**: Using the `reduce` method on the array to accumulate an object with the desired data. 3. **For Loop**: Using a traditional `for` loop to iterate over the array's elements and add them to an object. **Pros/Cons of each approach:** * **ForEach**: * Pros: Easy to read and write, concise code. * Cons: May not be as efficient as other methods for large datasets. * **Reduce**: * Pros: Efficient and can handle large datasets well. Also, it's a functional programming approach which some developers prefer. * Cons: Can be less readable than `forEach` or `for` loops for simple use cases. * **For Loop**: * Pros: Can be more efficient than `forEach` for very large datasets and is easy to read. It's also a traditional looping approach which many developers are familiar with. * Cons: May not be as concise as `forEach` or `reduce`, and can lead to more boilerplate code. **Other considerations:** * **Library usage**: None in this benchmark, so no library explanation is needed. * **Special JS feature or syntax**: The use of arrow functions (`=>`) in the `forEach` test case. This is a modern JavaScript feature (introduced with ES6) that allows for concise function definitions. **Other alternatives:** You could also compare other methods such as using a `map` function, or even using a library like Lodash's `each` method if you want to see how they perform in comparison.
Related benchmarks:
forEach vs reduce vs for - fill object
flatMap vs reduce using push
flatMap vs reduce using push spread
flatMap vs reduce, but without copying the array in each iteration
Comments
Confirm delete:
Do you really want to delete benchmark?