Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs reduce vs map vs filter vs for..of
(version: 0)
Comparing performance of:
forEach vs reduce vs map vs filter vs for vs forof
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 12345; i++) { arr[i] = i; } function someFn(i) { return (i * 3 * 8 / 1200 * 0.002 / 40 * 0.2); } var sumForEach = 0, sumReduce = 0, sumMap = 0, sumFilter = 0, sumFor = 0, sumForOf =0;
Tests:
forEach
arr.forEach(item => sumForEach += someFn(item));
reduce
sumReduce = arr.reduce((lastValue, item) => { return sumReduce += someFn(item); });
map
arr.map(item => (sumMap += someFn(item)));
filter
arr.filter(item => (sumFilter += someFn(item)));
for
for (var j = 0; j < arr.length; j++) { sumFor += arr[j]; }
forof
for (let a of arr) { sumForOf += a; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
forEach
reduce
map
filter
for
forof
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 dive into the Benchmark Definition and analyze what's being tested. The benchmark measures the performance of various JavaScript iteration methods: `forEach`, `reduce`, `map`, `filter`, and `for` (including `for-of`). The test case creates an array of 12,345 elements, populated with integers from 0 to 11,444. Each iteration method is used to calculate a sum by applying the same function (`someFn`) to each element in the array. Here's a brief explanation of each approach: 1. `forEach`: Iterates over the array using the `forEach` method, calling the callback function for each element. 2. `reduce`: Accumulates the sum by reducing the array to a single value using the `reduce` method, with an initial value of 0. 3. `map`: Creates a new array by mapping the original array's elements to a new value using the `map` method, accumulating the sums in the process. 4. `filter`: Filters the array to only include elements that meet a condition (in this case, always true) and accumulates the sums in the filtered array. 5. `for`: Iterates over the array manually using a traditional `for` loop, adding each element to the sum. **Options compared:** The benchmark is comparing the performance of these five iteration methods on an array of 12,345 elements. The comparison aims to identify which method is most efficient in terms of execution time and memory usage. **Pros and Cons:** * **forEach**: Pros - easy to read and write, straightforward syntax. Cons - can be less efficient due to the overhead of callback functions. * **reduce**: Pros - concise syntax, accumulating sums can be easier than iterating over the array. Cons - requires initial value and callback function, can be error-prone. * **map**: Pros - creates a new array with transformed values, easy to chain methods. Cons - creates an additional array, which can be memory-intensive. * **filter**: Pros - filters out unwanted elements, easy to write conditional logic. Cons - also creates an additional array and can be slower due to the filtering process. * **for**: Pros - manual control over iteration, no overhead from method calls. Cons - verbose syntax, prone to errors. **Library usage:** There is no explicit library used in the benchmark code. However, the `map` method uses the `Array.prototype.map()` method, which is a built-in JavaScript method. **Special JS feature or syntax:** * The `for-of` loop (used in the "forof" test case) was introduced in ECMAScript 2015 (ES6). It provides a more concise and readable way to iterate over arrays without manual indexing. * The use of arrow functions (`=>`) is also a modern JavaScript feature, introduced in ECMAScript 2015. **Other alternatives:** To further improve performance or provide alternative iteration methods, other options could be explored, such as: * Using `Array.prototype.forEach()` with a custom callback function * Implementing a custom iterator using the `Symbol.iterator` method and `next()` function * Utilizing Web Workers or parallel processing for multi-threaded execution
Related benchmarks:
forEach vs reduce vs map vs filter vs for
forEach vs reduce vs map vs filter vs for tiny
forEach vs reduce vs map vs filter vs for v2292U9I2JIR2J0IEJ02JE0IJ20EJ
forEach vs reduce vs map vs filter vs for (slightly optimized for, fixed fn)
forEach vs reduce vs map vs filter vs for (slightly optimized for (fixed))
Comments
Confirm delete:
Do you really want to delete benchmark?