Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array sum with 7 diff loops
(version: 0)
for..;; vs for..in vs for..of vs forEach vs map vs reduce
Comparing performance of:
regular for (;;) vs for..in vs for..of vs forEach() vs map() vs reduce()
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Int32Array.from({ length: 1000 }, (_, i) => i), fun = n => 3 * n >> 1, res = 0;
Tests:
regular for (;;)
const array = arr, len = arr.length; for (var i = 0; i < len; res += fun(array[i++])); console.log('for (;;):', res, res = 0);
for..in
const array = arr; for (const i in array) res += fun(array[i]); console.log('for (in):', res, res = 0);
for..of
for (const v of arr) res += fun(v); console.log('for (of):', res, res = 0);
forEach()
arr.forEach(v => res += fun(v)); console.log('forEach():', res, res = 0);
map()
arr.map(v => res += fun(v)); console.log('map():', res, res = 0);
reduce()
res = arr.reduce((a, v) => a + v); console.log('reduce():', res, res = 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
regular for (;;)
for..in
for..of
forEach()
map()
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 break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark is designed to compare the performance of different ways to iterate over an array and calculate its sum. The script preparation code creates an array of 1000 integers, calculates the square of each element (using the `fun` function), and sums up the results. The description mentions six approaches: `for (;;)` , `for..in`, `for..of`, `forEach()`, `map()`, and `reduce()`. **Test Cases** The benchmark consists of seven test cases, each representing one of the mentioned approaches: 1. **regular for (;;)**: A traditional `for` loop that uses an index variable to iterate over the array. 2. **for..in**: A `for...in` loop that iterates over the array's indices, using the `in` keyword to access each element. 3. **for..of**: A `for...of` loop that directly iterates over the array elements, without accessing an index variable. 4. **forEach()**: The `Array.prototype.forEach()` method is called on the array, passing a callback function to process each element. 5. **map()**: The `Array.prototype.map()` method is called on the array, passing a callback function to transform each element (in this case, squaring it). 6. **reduce()**: The `Array.prototype.reduce()` method is called on an initial value of 0, passing a callback function to accumulate the sum of the elements. **Libraries and Features** * None explicitly mentioned, but `forEach()`, `map()`, and `reduce()` are built-in methods of the Array prototype. * No special JavaScript features or syntax are used in this benchmark. **Pros and Cons** Here's a brief overview of the pros and cons for each approach: 1. **regular for (;;)**: Easy to understand, but can be slow due to the overhead of incrementing an index variable. 2. **for..in**: Less efficient than traditional loops because it uses the `in` keyword to access elements, which involves a string conversion. 3. **for..of**: Generally faster and more modern than traditional loops or `for..in`, but may have minor performance variations depending on the engine. 4. **forEach()**: Convenient and easy to use, but can be slower than traditional loops due to the overhead of method invocation. 5. **map()**: Transformative and convenient, but can be slower than traditional loops because it creates a new array with transformed elements. 6. **reduce()**: Accumulative and concise, but can be slower than traditional loops because it uses an accumulator value. **Other Alternatives** If you'd like to explore other approaches, consider: * Using `array.prototype.forEach()` with a custom callback function * Utilizing `Array.prototype.reduce()` with a custom initial value * Employing a different data structure, such as a linked list or queue * Implementing your own iterative loop using a pointer or index variable Keep in mind that the performance differences between these approaches may vary depending on the specific use case and engine.
Related benchmarks:
map vs forEach vs for loop
sum calculation: forEach vs for vs forof vs reduce 2
for of vs Array.reduce vs Array.forEach vs for i for summing and array of integers
for vs forEach vs for-of vs for-reverse (short array summing)
Array.prototype.reduce() vs for loop sum
Comments
Confirm delete:
Do you really want to delete benchmark?