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
(version: 0)
A test summing 1000 random numbers, 1 - 10000
Comparing performance of:
Array.reduce vs for loop vs Array.forEach vs for of loop
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 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:
Array.reduce
var redeuceResult=0; redeuceResult = doRedeuce(arrRandom);
for loop
var loopResult=0; loopResult = doForLoop(arrRandom);
Array.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
Array.reduce
for loop
Array.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 dive into the explanation of the provided benchmark. **Overview** The test compares the performance of four different approaches to sum an array of 1000 random integers: 1. `Array.prototype.reduce()` 2. A traditional `for` loop 3. `Array.prototype.forEach()` with a callback function 4. An `Iterator Protocol` (ECMAScript 2020+) implemented `for...of` loop Each test case has a separate benchmark definition, which is executed to measure the execution time. **Options Compared** 1. **Array.prototype.reduce()**: This method accumulates values in an array and returns the accumulated value. In this case, it's used to sum all elements of the input array. 2. **Traditional `for` loop**: A classic way to iterate over an array using a loop variable. 3. **Array.prototype.forEach()` with a callback function**: This method iterates over an array and executes a provided callback function for each element in the array. 4. **Iterator Protocol (ECMAScript 2020+) implemented `for...of` loop**: A modern way to iterate over arrays, introduced in ECMAScript 2020+. It's more concise and efficient than traditional loops. **Pros and Cons** 1. **Array.prototype.reduce()**: * Pros: Concise, easy to read, and expressive. * Cons: May be slower due to the overhead of creating an accumulator object. 2. **Traditional `for` loop**: * Pros: Fast, reliable, and easy to optimize. * Cons: Can be verbose and less readable than other options. 3. **Array.prototype.forEach()` with a callback function**: * Pros: Easy to read, concise, and flexible. * Cons: May have performance overhead due to the creation of an array of accumulated values. 4. **Iterator Protocol (ECMAScript 2020+) implemented `for...of` loop**: * Pros: Modern, efficient, and concise. * Cons: Requires ECMAScript 2020+ support, which may not be available in all browsers. **Library/Features Used** 1. The `doRedeuce()`, `doForLoop()`, and `doForEach()` functions use the `Array.prototype.reduce()`, traditional `for` loop, and `Array.prototype.forEach()` methods, respectively. 2. The `doForOfLoop()` function uses the new `Iterator Protocol` (ECMAScript 2020+) implemented `for...of` loop. **Special JavaScript Features/Syntax** This benchmark does not use any special JavaScript features or syntax other than the ECMAScript 2020+ Iterator Protocol, which is widely supported in modern browsers and Node.js. **Other Alternatives** For this specific test, the alternatives are: 1. Using a `while` loop instead of a traditional `for` loop. 2. Using the `map()` method to sum all elements in the array (although this would return a new array with summed values). 3. Using a library like Lodash or Ramda for functional programming approaches. These alternatives might be suitable for different use cases, but for this specific test, the four options mentioned are the most relevant and comparable approaches.
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?