Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.reduce vs for loop vs Array.forEach experiments
(version: 3)
A test summing 1000 random numbers, 1 - 10000
Comparing performance of:
reduce (arrow function) vs for loop vs forEach vs reduce
Created:
7 years ago
by:
Registered User
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 doRedeuce(pArray, start) { return pArray.reduce(function(accum, curr) { return accum + curr; }, start); } function doRedeuceArrowFunc(pArray, start) { return pArray.reduce((accum, curr) => accum + curr, start); } 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((item) => { accum += item; }); }
Tests:
reduce (arrow function)
var reduceResult=0; redeuceResult = doRedeuceArrowFunc(arrRandom, 0);
for loop
var loopResult=0; loopResult = doLoop(arrRandom);
forEach
var forEachResult=0 forEachResult = doForEach(arrRandom)
reduce
var reduceResult=0; redeuceResult = doRedeuce(arrRandom, 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
reduce (arrow function)
for loop
forEach
reduce
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 world of JavaScript microbenchmarks. **Benchmark Overview** The provided benchmark compares three different approaches to sum an array of 1000 random integers: `Array.reduce`, `for` loop, and `Array.forEach`. The goal is to determine which approach is the fastest. **Options Compared** 1. **Array.reduce**: This method uses a callback function to reduce the array to a single value, accumulating the sum. 2. **For Loop**: A traditional `for` loop is used to iterate over the array, adding each element to a running total. 3. **Array.forEach**: The `forEach` method is used to execute an action on each element in the array, in this case, adding the element to a running total. **Pros and Cons** * **Array.reduce**: + Pros: Concise, expressive, and easy to read. + Cons: Can be slower than traditional loops due to the overhead of function calls. * **For Loop**: + Pros: Fast, control over iteration order, and no function call overhead. + Cons: Verbose, harder to read, and more error-prone. * **Array.forEach**: + Pros: Concise, expressive, and easy to read. + Cons: Slower than traditional loops due to the overhead of function calls. **Library/Function Used** In this benchmark, `Math.floor` is used to generate random integers between 1 and 10000. No external libraries are required. **Special JS Feature/Syntax** None mentioned in the provided code. However, it's worth noting that modern JavaScript supports arrow functions (`=>`) which can simplify code and improve readability. **Other Alternatives** If you wanted to include additional approaches, some alternatives could be: * `Array.prototype.reduceRight` * Using a library like Lodash for more concise implementation * Using SIMD instructions (e.g., with WebAssembly) for parallel processing Keep in mind that these alternatives might not be directly applicable to this specific benchmark. **Benchmark Preparation Code** The provided script preparation code creates an array of 1000 random integers between 1 and 10000, defines three functions (`doRedeuce`, `doRedeuceArrowFunc`, and `doLoop`) for each approach, and initializes variables for the results. The HTML preparation code is empty in this case. **Individual Test Cases** Each test case executes one of the three approaches (reduce, for loop, or forEach) on the generated array, accumulating the sum in a variable.
Related benchmarks:
map vs forEach 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?