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
gemma2:9b
, generated one year ago):
This benchmark compares three different approaches to filling an object with values based on an array: **1. `forEach`:** Iterates through each element of the array using the `forEach` method and for each element, it adds a property to the `objForEach` object with a value indicating its existence (`{ exists: true }`). **2. `reduce`:** Uses the `reduce` method to iterate through the array and accumulate the result in an object. For each element, it sets a property on the accumulating object representing the existence of that element. **3. `for` Loop:** Employs a traditional `for` loop to iterate over the array indices and uses the loop variable `j` to access elements from `arr`. It then adds properties to `objFor` based on each array element's value. **Pros & Cons:** * **`forEach`:** * **Pros:** Concise syntax, readable for beginners. * **Cons:** Can be less efficient than other methods when working with large arrays and performing complex operations on each element. * **`reduce`:** * **Pros:** Often very efficient for tasks that involve accumulating a result from an array (like summing values or creating a new object). Provides a more functional programming style. * **Cons:** Can be less intuitive for beginners compared to `forEach`. * **`for` Loop:** * **Pros:** Fine-grained control over iteration, can be optimized for specific scenarios. * **Cons:** Can be verbose and harder to read than higher-order methods like `forEach` and `reduce`. **Other Considerations:** * In this particular benchmark, the focus is on performance when creating an object where the keys are the array elements and the values indicate existence. * The choice between these methods often depends on the specific task and the size of the array being processed. * If you're primarily concerned with readability and simplicity, `forEach` might be a good choice. **Alternatives:** * **Object Literal Notation:** Could be used to create the object directly in a single line, but it might not be as flexible for more complex scenarios: `const obj = { 0: true, 1: true, ... 49999: true };`. Let me know if you have any other questions or would like more details about a specific aspect!
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?