Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object from array forEach vs reduce
(version: 0)
Comparing performance of:
reduce vs foreach
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = [{id:"1",name:"aaa"}, {id:"2",name:"bbb"}, {id:"3",name:"ccc"}, {id:"4",name:"ddd"}, {id:"5",name:"eee"}, {id:"6",name:"fff"}]; var rowkey = 'id,name'; var rkarray = rowkey.split(',') var isComposite = (rkarray.length>1);
Tests:
reduce
arr.reduce((acc,row) => { acc[isComposite ? rkarray.map((col)=>row[col]).join(',') : row[rowkey]] = row; return acc; },{})
foreach
var acc={}; arr.forEach(row => acc[isComposite ? rkarray.map((col)=>row[col]).join(',') : row[rowkey]] = row);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce
foreach
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 provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of two approaches for processing an array of objects: `forEach` (using JavaScript's built-in `forEach()` method) and `reduce`. The test case uses a predefined JSON object with six elements, each containing `id` and `name` properties. The goal is to determine which approach is faster. **Options Being Compared** Two options are being compared: 1. **forEach**: This approach iterates over the array using the `forEach()` method, which calls a provided callback function for each element in the array. 2. **reduce**: This approach uses the `reduce()` method to iterate over the array and accumulate values. **Pros and Cons** * **forEach**: Pros: * Easier to understand and maintain due to its simplicity and readability. * Suitable for iterating over arrays with known, static elements. * **forEach**: Cons: * Less efficient than `reduce` because it creates a new array in each iteration and performs additional overhead from the `forEach()` method itself (e.g., checking the length of the array). * **reduce**: Pros: * More efficient than `forEach`, especially for large datasets, since it accumulates values in place. * Suitable for iterative processes where you need to keep track of accumulated results. However, reducing performance comes at the cost of readability. **Library Usage** There is no explicit library usage mentioned in this benchmark; however, JavaScript's built-in `forEach` and `reduce` functions are part of the standard library. **Special JS Features or Syntax** * The JSON object with six elements (`arr`) represents a real-world data structure. * Using template literals (e.g., `${row[col]}`) is a convenient way to access nested object properties in JavaScript. Now that we have gone through what each part of the benchmark means, let's explore other alternatives for similar operations: Other alternatives for `forEach` include: 1. Using a simple loop (`for` or `while`) instead of `forEach`. 2. Employing an iterative approach using arrow functions (which are equivalent to callbacks). Alternative implementations of `reduce` include: * **Array.prototype.reduce.call()**: Instead of chaining the `reduce()` method, this uses the `call()` method to invoke it. * Using a custom implementation of a reduction function. Here's how you might implement these alternatives using the same JSON object (Note: This will be in a separate test case or file because of differences in syntax): ```javascript // Example usage of forEach alternative: var arr = [{id:\"1\",name:\"aaa\"}, {id:\"2\",name:\"bbb\"}, {id:\"3\",name:\"ccc\"}, {id:\"4\",name:\"ddd\"}, {id:\"5\",name:\"eee\"}, {id:\"6\",name:\"fff\"}]; for (var i=0; i < arr.length; i++) { console.log(arr[i]); } // Example usage of reduce alternative: function reduceCustom(acc, row) { if (acc == null) { acc = {}; } acc[row.id + ',' + row.name] = row; return acc; } var reducedAcc = arr.reduce(reduceCustom); console.log(reducedAcc); ```
Related benchmarks:
forEach vs reduce vs for - fill object with object
Object.fromEntries on array vs reduce on array
for-in vs object.keys vs object.values for objects perf 5
Object.fromEntries vs reduce round 2
Object.fromEntries(Array.map) vs Array.reduce (with different methods)
Comments
Confirm delete:
Do you really want to delete benchmark?