Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce vs for loop vs forEach 100 count
(version: 0)
A test summing 100 random numbers, 1 - 10000
Comparing performance of:
reduce vs for loop vs forEach
Created:
5 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<100; 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 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 = doForEach(arrRandom)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
reduce
for loop
forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
2950215.8 Ops/sec
for loop
9006720.0 Ops/sec
forEach
3806905.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different JavaScript iteration methods is crucial in understanding how to optimize code for performance-critical sections. **Benchmark Overview** The benchmark tests three popular JavaScript iteration methods: `reduce`, `for loop`, and `forEach`. The test case generates an array of 1000 random integers between 1 and 10,000 using a for loop. Each method is then used to sum these numbers, and the execution time is measured. **Test Case Breakdown** 1. **`doRedeuce(pArray)`**: This function uses the `reduce()` method with a custom callback function (`reduceCallback`) that adds two numbers together. 2. **`doLoop(pArray)`**: This function uses a traditional for loop to iterate over the array, adding each number to a running total (`accum`). 3. **`doForEach(pArray)`**: This function uses the `forEach()` method with a callback function that also adds each number to a running total (`accum`). **Comparison of Iteration Methods** 1. **`reduce`**: * Pros: Efficient, concise, and easy to read. * Cons: Can be less intuitive for beginners, as it requires understanding the callback function's purpose. * Library used: `Array.prototype.reduce()`, a built-in JavaScript method. 2. **`for loop`**: * Pros: Well-known, easy to understand, and allows for manual control over iteration variables. * Cons: Can be verbose, prone to errors due to indexing issues, and slower compared to other methods. 3. **`forEach`**: * Pros: Simple, concise, and widely supported by modern browsers and engines. * Cons: Similar to `reduce`, can require understanding the callback function's purpose. **Special Considerations** This benchmark does not use any special JavaScript features or syntax that would impact its performance comparison. However, it's essential to note that the choice of iteration method often depends on the specific problem requirements, data structure, and performance constraints. **Alternatives to `forEach` and `reduce`** 1. **Using `for...of` loop**: This is a newer iteration method (ECMAScript 2017+) that provides similar efficiency to `forEach` but with more manual control. 2. **Using `Array.prototype.map()` or `Array.prototype.filter()`**: These methods can be used in conjunction with `reduce()` or `for loop` to achieve similar results, often with better performance. **Other Optimization Considerations** 1. **Avoid using `eval()`**: This method is generally discouraged due to its slow execution and potential security risks. 2. **Use native array methods when possible**: Built-in methods like `filter()`, `map()`, and `reduce()` are optimized for performance and provide better results compared to custom implementations. When choosing an iteration method, consider the trade-offs between code readability, maintainability, and performance. This benchmark provides a good starting point for comparing these popular methods in JavaScript.
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?