Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.reduce vs for loop vs Array.forEach vs for of loop (debugged)
(version: 0)
A test summing 1000 random numbers, 1 - 10000
Comparing performance of:
reduce vs for loop vs forEach vs for of loop
Created:
6 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 doForLoop(pArray) { var accum = 0; var arrLength = pArray.length; for(var intCtr=0; intCtr<arrLength; intCtr++) { accum += pArray[intCtr]; } return accum; } function doForOfLoop(pArray) { var accum = 0; for(var value of pArray) { accum += value; } 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 = doForLoop(arrRandom);
forEach
var forEachResult=0 forEachResult = doForEach(arrRandom)
for of loop
var forOfResult=0 forOfResult = doForOfLoop(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 loop
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):
Measuring the performance of different JavaScript looping constructs is crucial for understanding the language's efficiency and optimizing code. **Benchmark Overview** The provided benchmark compares four common looping methods in JavaScript: 1. `Array.prototype.reduce()` 2. Traditional `for` loop 3. `Array.prototype.forEach()` with a callback function 4. A newer looping construct called `for...of` **Options Compared** Each option is compared based on their execution performance, measured by the number of executions per second (ExecutionsPerSecond). **Comparison Options and Their Pros/Cons:** 1. **`Array.prototype.reduce()`**: * Pros: Can be more efficient for array operations since it avoids the need to manually increment an index. * Cons: May have higher overhead due to its functional programming nature. 2. **Traditional `for` loop**: * Pros: Highly efficient, easy to read and understand, and can take advantage of compiler optimizations. * Cons: Requires manual indexing, which can be error-prone. 3. **`Array.prototype.forEach()` with a callback function**: * Pros: Convenient for iterating over arrays, but may have higher overhead due to the use of a callback. * Cons: May not be as efficient as traditional loops or `for...of`. 4. **`for...of` looping construct**: * Pros: Modern and concise, easy to read, and optimized for performance in modern browsers. * Cons: Limited browser support, may require ES6+ syntax. **Library Used** In the provided benchmark, no specific libraries are used beyond `Math.random()` for generating random numbers. However, some JavaScript engines (like V8) might have additional optimizations or features not explicitly mentioned here. **Special JS Features** The test cases utilize a newer looping construct called `for...of`, which is part of ES6+ syntax and has better performance in modern browsers compared to traditional loops. **Alternatives** If you're interested in exploring alternative looping constructs, consider: * **`Array.prototype.map()`**: Can be used for transforming arrays without re-indexing. * **`Set` data structures**: Efficient for fast lookups and insertions. * **`Async/await`**: Suitable for handling asynchronous operations with a more synchronous-like syntax. **Additional Considerations** When measuring performance in JavaScript, keep the following in mind: * **Browser differences**: Results may vary depending on the browser's engine, version, and configuration. * **Loop optimization techniques**: Use techniques like caching, memoization, or parallel processing to optimize loop performance. * **Function call overhead**: Excessive function calls can introduce significant overhead; consider using native functions instead of wrapped versions. The provided benchmark should give you a good starting point for understanding the relative performance of different looping constructs in JavaScript.
Related benchmarks:
map vs forEach vs for loop
Array.reduce vs for loop vs Array.forEach vs for of loop
Array.reduce vs for loop vs Array.forEach vs for of loop
Array.reduce vs for loop vs Array.forEach vs for of loop - 10000
Array.reduce vs for loops vs Array.forEach
Comments
Confirm delete:
Do you really want to delete benchmark?