Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.reduce vs for loop vs Array.forEach
(version: 0)
A test summing 1000 random numbers, 1 - 10000
Comparing performance of:
reduce vs for loop vs forEach
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
// Create an array of 1000 random intergers between 1 and 10000 var arrRandom = []; for(var intCtr=0; intCtr<1000; intCtr++) { arrRandom.push(Math.floor(Math.random() * Math.floor(10000))); } function reduceCallback(accum, curr) { return accum+curr; } function doRedeuce(pArray) { return pArray.reduce(reduceCallback); } function doLoop(pArray) { var accum = 0; for(var intCtr=0; intCtr<pArray.length; intCtr++) { accum += pArray[intCtr]; } return accum; } function doForEach(pArray) { var accum = 0; pArray.forEach(function(item) { accum += item; }); }
Tests:
reduce
var redeuceResult=0; redeuceResult = doRedeuce(arrRandom);
for loop
var loopResult=0; loopResult = doLoop(arrRandom);
forEach
var forEachResult=0 forEachResult = doForEach(arrRandom)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
reduce
for loop
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Browser/OS:
Chrome 142 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
2584525.0 Ops/sec
for loop
3220216.0 Ops/sec
forEach
543166.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Definition** The benchmark is comparing three different approaches to sum an array of 1000 random numbers: 1. `Array.reduce()`: A method that applies a reduction function to each element in the array, accumulating the result. 2. `for` loop: A traditional loop that iterates over the array, adding each element to a running total. 3. `Array.forEach()`: A method that calls a provided callback function for each element in the array. **Options being compared** The three options are being compared in terms of their performance (measured by "ExecutionsPerSecond") on a specific browser and device platform combination. **Pros and Cons** * **`Array.reduce()`**: This approach is concise and can be more memory-efficient than traditional loops, as it only requires a single accumulator variable. However, it may not be suitable for very large arrays or performance-critical applications. * **`for` loop**: This approach is straightforward and easy to understand, but it can be slower and more memory-intensive than `Array.reduce()`, especially for large arrays. * **`Array.forEach()`**: This approach is similar to the traditional loop, but it's often considered more elegant and easier to read. However, it may not be as performant as the other two options. **Library usage** The benchmark uses the following libraries: * None explicitly mentioned in the JSON, but the `Math.random()` function is used for generating random numbers. * No external libraries are required for this specific benchmark. **Special JS features/syntax** There's no special JavaScript feature or syntax being tested in this benchmark. The code uses standard JavaScript syntax and built-in functions. **Other alternatives** If you're interested in exploring alternative approaches, consider the following: * Using `Array.prototype.every()` or `Array.prototype.some()` instead of `forEach()`, which can provide similar results but with different performance characteristics. * Using a parallel processing library (e.g., Web Workers) to run multiple iterations of each benchmark simultaneously, which can improve overall performance and provide more accurate comparisons. Keep in mind that these alternatives may require significant changes to the benchmark code and might not be suitable for all use cases.
Related benchmarks:
map vs forEach vs for loop
reduce vs for loop
reduce vs for loop
Array.reduce vs for loop vs Array.forEach experiments - Fix Foreach as function
Array.reduce vs for loops vs Array.forEach
Comments
Confirm delete:
Do you really want to delete benchmark?