Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.reduce vs for loop vs Array.forEach v2
(version: 0)
A test summing 1000 random numbers, 1 - 10000
Comparing performance of:
reduce vs for loop vs forEach
Created:
7 years ago
by:
Registered User
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) { return pArray.reduce((accum, curr) => accum + curr); } function doLoop(pArray) { let accum = 0; for(let intCtr=0; intCtr < pArray.length; intCtr++) { accum += pArray[intCtr]; } return accum; } function doForEach(pArray) { let accum = 0; pArray.forEach(item => { 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)
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):
Let's break down the provided JSON and explain what is tested on MeasureThat.net. **Benchmark Definition** The benchmark is designed to compare the performance of three different approaches for summing an array of random integers: 1. `Array.reduce` 2. For loop 3. `Array.forEach` The script preparation code creates an array of 1000 random integers between 1 and 10,000, and defines three functions: `doRedeuce`, `doLoop`, and `doForEach`. These functions perform the summing operations using the respective approaches. **Options Compared** The benchmark compares the performance of each approach: * `Array.reduce`: uses the built-in `reduce` method to sum the array elements. * For loop: uses a traditional for loop to iterate through the array elements and add them up. * `Array.forEach`: uses the `forEach` method to iterate through the array elements, but in this case, it's modified to also perform the summing operation. **Pros and Cons of Each Approach** 1. **`Array.reduce`**: * Pros: concise, efficient, and easy to read. * Cons: may not be as intuitive for some developers, especially those without experience with functional programming. 2. **For loop**: * Pros: straightforward, easy to understand, and suitable for complex logic. * Cons: can be slower due to the overhead of explicit looping and conditional statements. 3. **`Array.forEach`**: * Pros: concise, efficient, and suitable for simple transformations or operations. * Cons: may not be as intuitive for summing operations, especially without modification. **Library Used** None is explicitly mentioned in the provided code, but `Math.floor` is used to generate random numbers. However, there are no libraries that are imported or referenced in the script preparation code. **Special JS Feature/Syntax** No special JavaScript features or syntaxes are used in this benchmark. The focus is on comparing different approaches for summing an array of integers. **Other Alternatives** To measure the performance of these approaches, you could also consider: * Using a similar benchmarking framework like Benchmark.js or js-benchmark * Adding more test cases to cover different scenarios, such as sorting or searching algorithms * Comparing the performance with other languages or frameworks that implement similar methods (e.g., Python's built-in `sum` function) Keep in mind that MeasureThat.net is a specialized platform designed specifically for JavaScript benchmarking. If you're interested in comparing the performance of these approaches in a different context, you may want to consider alternative tools and frameworks.
Related benchmarks:
map vs forEach vs for loop
Array.reduce vs for loop vs Array.forEach experiments
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?