Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sum an array2
(version: 2)
Comparing performance of:
Reduce vs for..of vs for vs foreach vs for..of with const vs for again
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = []; for (let j=0; j < 1000000; j++ ) { arr.push(j); }
Tests:
Reduce
const r = arr.reduce( (p,c) => p+c, 0);
for..of
let r=0; for (v of arr) { r+=v; }
for
let r = 0; const l = arr.length; for (var i=0; i < l; i++ ) { r += arr[i]; }
foreach
let r = 0; arr.forEach( (v) => r += v );
for..of with const
let r=0; for (const v of arr) { r+=v; }
for again
let r = 0, i=0; const l = arr.length; for (;i < l;) { r += arr[i++]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Reduce
for..of
for
foreach
for..of with const
for again
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 benchmark and explain what's being tested. **Benchmark Preparation Code** The script preparation code creates an array `arr` with 1,000,000 elements, each containing a sequential number from 0 to 999,999. This is done using a `for` loop that pushes each number onto the array. **Test Cases** The benchmark consists of six test cases, each measuring the performance of a different JavaScript iteration method: 1. `Reduce`: Uses the `reduce()` method to sum up all elements in the array. 2. `for..of with const`: Uses a `for...of` loop with `const` declaration to iterate over the array and sum up its elements. 3. `foreach`: Uses the `forEach()` method to iterate over the array and sum up its elements. 4. `for`: Uses a traditional `for` loop to iterate over the array and sum up its elements. 5. `for again`: Similar to the previous one, but uses an index variable (`i`) instead of a direct reference to the element. 6. `for..of`: Another `for...of` loop without `const`, used for comparison. **Library Usage** None of these test cases explicitly use any libraries or external dependencies. **JS Features/Syntax** The only notable feature is the use of `const` declaration in some test cases (`for..of with const` and another `for...of`). This is a relatively modern JavaScript feature that declares variables as constant, ensuring they cannot be reassigned. Its usage helps prevent potential performance issues by avoiding unnecessary reassignments. **Options Compared** The benchmark compares the performance of six different iteration methods: 1. **Reduce()**: A built-in method for reducing an array to a single value. 2. **for...of with const**: An improved version of traditional `for` loops, providing better performance and readability. 3. **forEach()**: Another iteration method that can be used for summing up elements in an array. 4. **Traditional for loop**: A basic iteration method using a variable index. 5. **for again**: A variation of the traditional `for` loop with an additional index variable (`i`). 6. **for...of (non-const)**: Another iteration method similar to `for...of with const`, but without declaring variables as constant. **Pros and Cons** Here's a brief summary: * **Reduce()**: Simple, efficient, and widely supported. However, it may not be the best choice for large arrays or performance-critical applications. * **for...of with const**: Offers better performance and readability compared to traditional `for` loops. It also ensures variables are not reassigned, which can prevent performance issues in some cases. * **forEach()**: Similar to `for...of`, but may have additional overhead due to its method call. * **Traditional for loop**: The most basic iteration method, but can be slower and more error-prone compared to the others. * **for again**: A variation of traditional `for` loops with an additional index variable. It may not offer significant performance benefits over the other options. **Other Alternatives** Some alternatives to these iteration methods include: * Using `Array.prototype.map()` or `Array.prototype.every()` to achieve similar results. * Leveraging parallel processing libraries like Web Workers to speed up computationally intensive operations. * Optimizing the algorithm itself, if possible, to reduce overhead and improve performance. Keep in mind that the best approach will depend on the specific use case and requirements.
Related benchmarks:
for of vs Array.reduce vs Array.forEach vs for i for summing and array of integers
empty an array in JavaScript?(Yorkie)1
Iterate array backwards then clear
JS Arr Sums Bad For
Comments
Confirm delete:
Do you really want to delete benchmark?