Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.reduce vs for loop vs Array.forEach vs for of loopv6
(version: 0)
A test summing 1000 random numbers, 1 - 10000
Comparing performance of:
reduce vs for loop vs forEach vs for of loop
Created:
6 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):
**Benchmark Explanation** The provided JSON represents a JavaScript benchmark test that compares the performance of four different approaches to iterate over an array: 1. `Array.reduce()` 2. Traditional `for` loop 3. `Array.forEach()` 4. `for...of` loop (introduced in ECMAScript 2015) The test case creates an array of 1000 random integers between 1 and 10,000 and calculates the sum using each of these four approaches. **Approach Comparison** Here's a brief overview of each approach: ### 1. `Array.reduce()` `reduce()` is a method that applies a callback function to each element in an array, accumulating a result. In this test, it's used with a simple addition function (`accum + curr`). This approach is suitable for reducing arrays to a single value. Pros: concise, efficient Cons: limited control over iteration order ### 2. Traditional `for` loop A traditional `for` loop iterates over an array by specifying the index and using it in the loop condition. In this test, it's used with a similar accumulation logic as `reduce()`. This approach provides more control over the iteration process. Pros: straightforward, fine-grained control Cons: verbose, can be slow for large arrays ### 3. `Array.forEach()` `forEach()` is a method that executes a callback function for each element in an array, without accumulating a result. In this test, it's used with a simple addition function (`accum += item`). This approach is similar to the traditional `for` loop but uses the built-in `forEach()` method. Pros: concise, efficient Cons: no accumulation logic, limited control over iteration order ### 4. `for...of` loop (ECMAScript 2015) The `for...of` loop iterates over an array by specifying the iterable object and a callback function. In this test, it's used with a similar accumulation logic as `reduce()`. This approach provides more control over the iteration process compared to traditional `for` loops. Pros: concise, efficient Cons: requires ECMAScript 2015 support **Library Used** None of the provided approaches use any external libraries. However, if you were to extend this benchmark, you might consider using libraries like Lodash or Ramda for functional programming utilities. **Special JS Feature/Syntax** There is no explicit use of special JavaScript features or syntax in these benchmarks. However, keep in mind that some approaches may perform better or worse depending on the specific browser and JavaScript engine used. **Alternative Approaches** Other alternatives to consider when iterating over arrays include: * `Array.prototype.map()` * `Array.prototype.every()` * Closures * Generators When choosing an iteration approach, consider factors like performance, readability, and maintainability. The best approach will depend on the specific use case and requirements.
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?