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 Fixed
(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):
**Benchmark Overview** The provided benchmark measures the performance of different JavaScript iteration approaches: `Array.reduce`, `for loop`, `Array.forEach`, and `for of loop`. The test case consists of summing 1000 random numbers between 1 and 10,000 using each approach. **Iteration Approaches Compared** ### 1. Array.reduce * **Purpose**: Reduces an array to a single value, accumulating the values in the process. * **Pros**: Can be more concise and expressive for simple reductions. * **Cons**: May have higher overhead due to function calls and array indexing. ### 2. For Loop * **Purpose**: Iterates over an array using a traditional loop. * **Pros**: Can provide direct access to array elements and is often more readable for simple iterations. * **Cons**: Can be verbose and error-prone for larger datasets or complex logic. ### 3. Array.forEach * **Purpose**: Calls a callback function once for each element in an array, without returning any value. * **Pros**: Encapsulates iteration logic and can simplify code when dealing with arrays. * **Cons**: May not be suitable for all use cases, such as when needing to return values or access elements directly. ### 4. For Of Loop * **Purpose**: A more modern way of iterating over arrays using the "for...of" statement. * **Pros**: Can provide a more concise and readable alternative to traditional loops. * **Cons**: May not be supported in older browsers or environments. **Library and Special JavaScript Features** The benchmark uses the following library: * `Math.random()`: Returns a random number between 0 (inclusive) and 1 (exclusive). There are no special JavaScript features mentioned in this benchmark. The test cases focus on the iteration approaches themselves, without utilizing any advanced or proprietary features. **Benchmark Preparation Code Explanation** The preparation code generates an array of 1000 random integers between 1 and 10,000 using `Math.floor(Math.random() * Math.floor(10000))`. This ensures that each number is within the desired range. The script also defines four functions: * `reduceCallback`: A simple callback function that returns the sum of two numbers. * `doRedeuce`, `doForLoop`, and `doForOfLoop`: Functions that wrap the corresponding iteration approaches. **Individual Test Cases** Each test case executes one of the iteration approaches, using the generated array `arrRandom`. The results are stored in variables (`redeuceResult`, `loopResult`, `forEachResult`, and `forOfResult`) and returned by each function. **Latest Benchmark Results** The provided benchmark results show the execution performance (in executions per second) for each test case: * For Loop: 720,525.0625 * For Of Loop: 479,454.59375 * Array.forEach: 532,726.125 * Array.reduce: 62,852.03125 Note that the results may vary depending on the actual environment and browser used. **Alternative Approaches** Some alternative approaches to these iteration methods include: * Using `Array.prototype.every` or `Array.prototype.some` for conditional checks. * Employing `Map` or `Set` data structures for faster lookups or aggregations. * Leveraging `Promises` or `Async/Await` for asynchronous operations. However, these alternatives may not provide a direct performance comparison to the original benchmark approaches.
Related benchmarks:
map vs forEach vs for loop
reduce vs for loop
reduce vs for loop
Array.reduce vs for loop vs Array.forEach vs for of loop - 10000
Array.reduce vs for loops vs Array.forEach
Comments
Confirm delete:
Do you really want to delete benchmark?