Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.reduce vs for loop vs Array.forEach vs for...of
(version: 0)
A test summing 1000 random numbers, 1 - 10000
Comparing performance of:
reduce vs for loop vs forEach vs for...of
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<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 doForEach(pArray) { var accum = 0; pArray.forEach(function(item) { accum += item; }); } function doForOf(pArray) { var accum = 0; for (const item of pArray) { accum += item; } return accum; }
Tests:
reduce
var redeuceResult=0; redeuceResult = doRedeuce(arrRandom);
for loop
var loopResult=0; loopResult = doLoop(arrRandom);
forEach
var forEachResult=0 forEachResult = doForEach(arrRandom)
for...of
var forOfResult=0 forOfResult = doForOf(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
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 four different approaches to summing an array of 1000 random integers: `Array.reduce()`, `for` loop, `Array.forEach()`, and `for...of`. The test aims to measure which approach is the fastest. **Option Comparison** 1. **`Array.reduce()`**: This method uses a callback function to accumulate the values in the array. It's a concise and efficient way to perform aggregations. * Pros: Easy to implement, performs well for most use cases. * Cons: Can be slower than other methods if the callback is complex or expensive to compute. 2. **`for` loop**: This method uses a traditional `for` loop to iterate over the array and accumulate the values. * Pros: Well-known, easy to understand, and often faster for simple aggregations. * Cons: Can be verbose, prone to errors due to indexing issues. 3. **`Array.forEach()`**: This method uses a callback function similar to `Array.reduce()`, but is designed specifically for iterating over arrays without accumulating values. * Pros: Easy to implement, performs well for use cases where only iteration is required. * Cons: Slower than `for` loop or `Array.reduce()` due to the overhead of creating and destroying the callback context. 4. **`for...of`**: This method uses a modern `for...of` loop to iterate over the array, which provides better performance and readability than traditional `for` loops. * Pros: Fast, readable, and concise. * Cons: May not be supported in older browsers or environments. **Library Considerations** There is no explicit library being tested in this benchmark. However, `Array.forEach()` uses a built-in JavaScript method, while the other methods rely on implicit iteration. **Special JS Features** The test does not explicitly use any special JavaScript features like async/await, Web Workers, or modern syntax (e.g., arrow functions). However, it's worth noting that some modern browsers may optimize certain methods more efficiently due to their native implementation. **Other Alternatives** If you're interested in exploring alternative approaches for this specific task, consider the following: * Using `Promise.all()` and `reduce()` to parallelize computations * Employing a more specialized library like `lodash` or `ramda` for optimized aggregation functions * Utilizing Web Workers or async/await for concurrent processing Keep in mind that these alternatives might not provide significant performance gains in this specific benchmark, but they can be useful in broader applications. Now that we've explored the test case, do you have any specific questions about the findings or would you like me to elaborate on any of the points mentioned above?
Related benchmarks:
map vs forEach vs for loop
reduce vs for loop
Array.reduce vs for loop vs Array.forEach vs for of loop - 10000
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?