Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.reduce vs for loop vs Array.forEach -123
(version: 0)
A test summing 1000 random numbers, 1 - 10000
Comparing performance of:
reduce vs for loop vs forEach
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<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:
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):
I'd be happy to help explain the benchmark and its various components. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that tests three different approaches for summing up an array of numbers: `Array.reduce()`, `for` loop, and `Array.forEach()`. **Options Compared** Here's a breakdown of each option: 1. **`Array.reduce()`**: This method uses the `reduce()` function to apply a callback function to each element in the array, accumulating a result. 2. **`For Loop`**: This approach uses a traditional `for` loop to iterate over the array elements and accumulate the sum. 3. **`Array.forEach()`**: This method uses the `forEach()` function to iterate over the array elements and apply a callback function to each one. **Pros and Cons** Here's a brief overview of the pros and cons of each approach: 1. **`Array.reduce()`**: * Pros: Efficient, concise, and easy to read. * Cons: May be slower for large arrays due to overhead from creating an accumulator. 2. **`For Loop`**: * Pros: Control over indexing and variable scope. * Cons: Can be verbose and harder to read for complex logic. 3. **`Array.forEach()`**: * Pros: Simple, readable, and easy to use. * Cons: May have performance overhead due to the callback function. **Library and Purpose** In this benchmark, two libraries are used: 1. **`Math.random()`**: This is a built-in JavaScript library that generates random numbers. 2. No specific library is used for `Array.reduce`, `for loop`, or `Array.forEach`. However, these methods are part of the native JavaScript API. **Special JS Features and Syntax** The benchmark uses the following special JavaScript features: 1. **`let` and `const`**: These keywords are used to declare variables that are not hoisted. 2. **Template literals`: The `template literal` syntax is used to create a string with embedded expressions. 3. **Arrow functions**: The `=>` syntax is used to define small, single-expression functions. **Other Alternatives** For summing up an array of numbers, other alternatives might include: 1. **Using `Array.prototype.reduce()` and defining a custom callback function**. 2. **Using a library like Lodash**, which provides a `sum` function for working with arrays. 3. **Using a more functional programming approach**, such as using the `reduceRight()` method. Keep in mind that these alternatives might have different performance characteristics or trade-offs, and may not be suitable for all use cases.
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?