Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.reduce vs for loop vs Array.forEach vs decrement while 2
(version: 0)
A test summing 1000 random numbers, 1 - 10000
Comparing performance of:
reduce vs for loop vs forEach vs decrement while
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 reduceCallback(accum, curr, index) { accum[curr] = index; return accum; } function doRedeuce(pArray) { return pArray.reduce(reduceCallback, {}); } function doLoop(pArray) { var accum = {}; for(var intCtr=0; intCtr<pArray.length; intCtr++) { accum[pArray[index]] = index; } return accum; } function decrWhile(pArray) { var accum = {}; var index = pArray.length; while(index--) { accum[pArray[index]] = index; } return accum; } function doForEach(pArray) { var accum = {}; pArray.forEach(function(item, index) { accum[item] = index; }); 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);
decrement while
var decrWhileResult=0; decrWhileResult = decrWhile(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
decrement while
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/120.0
Browser/OS:
Firefox 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
7177.8 Ops/sec
for loop
0.0 Ops/sec
forEach
9286.2 Ops/sec
decrement while
9928.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explain what is being tested. **Benchmark Definition** The benchmark measures the performance of four different approaches to summing 1000 random numbers between 1 and 10,000: 1. `Array.reduce()`: uses the `reduce()` method to accumulate the sum. 2. `for loop`: uses a traditional `for` loop to iterate through the array and calculate the sum. 3. `Array.forEach()`: uses the `forEach()` method to iterate through the array and calculate the sum. 4. `decrement while`: uses a `while` loop with decrementing indices to accumulate the sum. **Options Compared** Each option is compared in terms of: * **Speed**: The time taken by each approach to execute the benchmark. * **Accuracy**: Whether the calculated sum matches the expected value (1000 times 10,000). **Pros and Cons of Each Approach** 1. `Array.reduce()`: * Pros: concise, efficient, and easy to implement. * Cons: may have performance issues if the array is large or if the reduction function is complex. 2. `for loop`: * Pros: well-known, widely supported, and easy to understand. * Cons: can be slow for large arrays due to overhead of loop control. 3. `Array.forEach()`: * Pros: concise and efficient, with built-in support for iteration. * Cons: may have performance issues if the callback function is complex or expensive. 4. `decrement while`: * Pros: low-level, flexible, and easy to optimize. * Cons: can be slower and more error-prone than higher-level approaches. **Library Used** None. **Special JS Features/ Syntax** None mentioned in this benchmark. **Other Alternatives** For similar benchmarks, you might consider: 1. `Array.prototype.every()`: a method that returns true if all elements pass the test (can be used for summing up to a certain value). 2. `Array.prototype.map()`: a method that applies a function to each element and returns a new array (can be used for transforming data before summing). Keep in mind that the choice of approach depends on the specific requirements and constraints of your project. This benchmark provides a general comparison of different methods, but you may need to choose an approach based on factors like performance, readability, and maintainability.
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?