Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.reduce vs for loop vs Array.forEach vs for of loop 2
(version: 0)
A test summing 1000 random numbers, 1 - 10000
Comparing performance of:
reduce vs for loop vs forEach vs for of loop
Created:
7 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 doForLoop(pArray) { var accum = 0; var arrLength = pArray.length; for(var intCtr=0; intCtr<arrLength; intCtr++) { accum += pArray[intCtr]; } return accum; } function doForOfLoop(pArray) { var accum = 0; for(var value of pArray) { accum += value; } 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 = doForLoop(arrRandom);
forEach
var forEachResult=0 forEachResult = doForEach(arrRandom)
for of loop
var forOfResult=0 forOfResult = doForOfLoop(arrRandom)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
reduce
for loop
forEach
for of loop
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 benchmark and its options. **Benchmark Overview** The benchmark is designed to compare the performance of four different approaches for summing up an array of random numbers: 1. `Array.prototype.reduce()` 2. Traditional `for` loop 3. `Array.prototype.forEach()` (note: this method does not return the accumulated value, but we're using it as if it did) 4. `for...of` loop **Options Compared** The benchmark compares the performance of each option by executing them multiple times and measuring their execution time per second. * `reduce()`: uses an arrow function to specify the callback function for the reduction * Traditional `for` loop: uses a traditional `for` loop with a counter variable * `forEach()` (as if it returned the accumulated value): uses the `forEach()` method without modifying it, as if it were returning the accumulated value * `for...of` loop: uses a `for...of` loop to iterate over the array **Pros and Cons of Each Approach** 1. `reduce()`: * Pros: concise and expressive code, avoids explicit loops * Cons: might be less efficient due to additional overhead 2. Traditional `for` loop: * Pros: efficient and well-established pattern, easy to understand * Cons: more verbose than `reduce()` or `forEach()` 3. `forEach()` (as if it returned the accumulated value): * Pros: similar to `reduce()` in syntax and intent, avoids explicit loops * Cons: not a standard method of `Array.prototype.forEach()`, might be less efficient due to additional overhead 4. `for...of` loop: * Pros: concise and expressive code, easy to understand * Cons: relatively new feature, might not be supported by older browsers or environments **Library Usage** In this benchmark, the `reduce()` method uses an arrow function to specify the callback function for the reduction. **Special JS Feature/Syntax** The benchmark does not use any special JavaScript features or syntax that are not widely supported (e.g., ES6+ syntax). However, it's worth noting that the `for...of` loop is a relatively new feature that was introduced in ECMAScript 2015. **Alternative Approaches** Other alternatives for summing up an array of numbers could include: * Using `Array.prototype.map()` and then calling `reduce()` on the resulting mapped array * Using a `while` loop or other iterative approach * Using a functional programming library like Lodash These alternative approaches might have different performance characteristics, syntax requirements, and ease of use compared to the options tested in this benchmark.
Related benchmarks:
map vs forEach vs for loop
reduce vs for loop
Array.reduce vs for loop vs Array.forEach vs for of loop - 10000
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?