Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.reduce vs for loop vs Array.forEach experiments - Fix Foreach as function
(version: 0)
A test summing 1000 random numbers, 1 - 10000
Comparing performance of:
reduce (arrow function) vs for loop vs forEach vs reduce
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 doRedeuce(pArray, start) { return pArray.reduce(function(accum, curr) { return accum + curr; }, start); } function doRedeuceArrowFunc(pArray, start) { return pArray.reduce((accum, curr) => accum + curr, start); } 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 (arrow function)
var reduceResult=0; redeuceResult = doRedeuceArrowFunc(arrRandom, 0);
for loop
var loopResult=0; loopResult = doLoop(arrRandom);
forEach
var forEachResult=0 forEachResult = doForEach(arrRandom)
reduce
var reduceResult=0; redeuceResult = doRedeuce(arrRandom, 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
reduce (arrow function)
for loop
forEach
reduce
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 Definition** The provided JSON represents a benchmark that compares three different approaches to summing an array of 1000 random integers: `Array.prototype.reduce`, `for` loop, and `Array.prototype.forEach`. **Options Compared** 1. **Array.prototype.reduce**: This method uses the reduce() function to iterate over the array and accumulate the values. There are two versions: * `doRedeuce(pArray, start)`: A traditional reducer function that takes an accumulator and a current value as arguments. * `doRedeuceArrowFunc(pArray, start)`: An arrow function-based reducer that uses the same logic as the traditional version but with a more concise syntax. 2. **for loop**: This approach uses a traditional `for` loop to iterate over the array and accumulate the values. 3. **Array.prototype.forEach**: This method iterates over the array using a callback function, but it does not provide an accumulation mechanism. **Pros and Cons** 1. **Array.prototype.reduce**: * Pros: + Concise syntax with arrow functions ( `doRedeuceArrowFunc` ) + Efficient execution, as it avoids unnecessary iterations * Cons: + May be less intuitive for developers without experience with reduce() 2. **for loop**: * Pros: + Well-known and widely supported + Easy to implement and understand * Cons: + May lead to performance issues if not implemented correctly (e.g., using `i < array.length` instead of `i >= 0`) 3. **Array.prototype.forEach**: * Pros: + Easy to implement and understand * Cons: + Does not provide an accumulation mechanism, making it less suitable for this benchmark **Library: Array.prototype.reduce()** The `reduce()` method is a built-in JavaScript method that applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value. It's commonly used for tasks like summing arrays or aggregating data. **Special JS feature: Arrow functions** Arrow functions (`() => { }`) are a concise syntax introduced in ECMAScript 2015. They allow defining small, one-line functions without the `function` keyword. In this benchmark, arrow functions are used to implement the `doRedeuceArrowFunc` reducer function. **Other Considerations** * The benchmark assumes that the input array is not empty and contains at least one element. * The use of `Math.random()` generates random integers for the array, which may affect performance due to the overhead of generating random numbers. * The benchmark runs on a Chrome 69 browser with a Windows Desktop platform. **Alternatives** If you're looking for alternative approaches or libraries, consider: 1. Lodash: A popular utility library that includes functions like `reduce()` and others. 2. Ramda: A functional programming library that provides a `reduce()` function and other useful tools. 3. Manual iteration using `for` loops or `while` loops. Keep in mind that the performance differences between these approaches may be small, depending on the specific use case and input data.
Related benchmarks:
map vs forEach vs for loop
reduce vs for loop
Array.reduce vs for loop vs Array.forEach experiments
Array.reduce vs for loops vs Array.forEach
Comments
Confirm delete:
Do you really want to delete benchmark?