Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.reduce vs for loop vs Array.forEach vs decrement while
(version: 0)
A test summing 1000 random numbers, 1 - 10000
Comparing performance of:
reduce vs for loop vs forEach vs decrement while
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 doLoop(pArray) { var accum = 0; for(var intCtr=0; intCtr<pArray.length; intCtr++) { accum += pArray[intCtr]; } return accum; } function decrWhile(pArray) { var accum = 0; var index = pArray.length; while(index--) { accum += pArray[index]; } 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 = decrWhile(arrRandom)
decrement while
var decrWhileResult=0; decrWhileResult = doForEach(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
decrement while
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:141.0) Gecko/20100101 Firefox/141.0
Browser/OS:
Firefox 141 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
1077420.4 Ops/sec
for loop
2392480.2 Ops/sec
forEach
1862912.9 Ops/sec
decrement while
488600.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The test measures the performance of four different approaches to summing 1000 random numbers: 1. `Array.reduce()` 2. For loop 3. `Array.forEach()` (with a custom decrementing index) 4. Custom "decrement while" loop **Approach 1: `Array.reduce()`** `Array.reduce()` is a built-in JavaScript function that reduces an array to a single value by applying a callback function to each element in the array. In this case, the callback function simply adds the current number to the accumulator. Pros: * Concise and readable code * Built-in functionality makes it easy to understand and implement Cons: * May have performance overhead due to the need to create an iterator object and iterate over the array * Can be slower than a custom loop for large arrays **Approach 2: For Loop** A traditional for loop iterates over the elements of an array, executing a block of code for each element. Pros: * Fast execution speed, as it doesn't require creating an iterator object or iterating over the array * Control over iteration order and index access Cons: * Can be verbose and less readable than other approaches * May not be suitable for large arrays due to stack size limitations **Approach 3: `Array.forEach()`** `Array.forEach()` is another built-in JavaScript function that executes a callback function for each element in an array. In this case, the callback function adds the current number to the accumulator. Pros: * Similar to `Array.reduce()`, but with a more concise syntax * Can be faster than `Array.reduce()` due to optimized iterator implementation Cons: * May have performance overhead due to the need to create an iterator object and iterate over the array * Custom decrementing index is not supported by default **Approach 4: Custom "Decrement While" Loop** This custom loop uses a decrementing index to access elements in the array, similar to a traditional for loop. Pros: * Fast execution speed, as it doesn't require creating an iterator object or iterating over the array * Control over iteration order and index access Cons: * May be less readable than other approaches due to the custom logic * Not supported by default in JavaScript, requiring manual implementation **Library Used** None explicitly mentioned, but `Math.random()` is used to generate random numbers. **Special JS Features/Syntax** None explicitly mentioned. **Alternative Approaches** Other approaches that could be tested include: * Using `Array.prototype.map()` with a callback function * Using `Array.prototype.every()` or `Array.prototype.some()` with a callback function * Using Web Workers for parallel computation Note that these alternatives may not provide significant performance improvements over the existing approaches and would require additional benchmarking to confirm.
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?