Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs reduce vs map vs filter vs for
(version: 0)
Comparing performance of:
forEach vs reduce vs map vs filter vs for
Created:
8 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;
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]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
forEach
reduce
map
filter
for
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
12 hours ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36
Browser/OS:
Chrome 148 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
826.3 Ops/sec
reduce
799.8 Ops/sec
map
801.2 Ops/sec
filter
761.0 Ops/sec
for
939.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided JSON data and its associated benchmark. **Benchmark Definition** The benchmark is designed to compare the performance of four different approaches for iterating over an array: `forEach`, `reduce`, `map`, and `filter`. The script prepares an array of 12345 elements, each with a specific calculation performed on it. The common function, `someFn(i)`, is applied to each element. **Options Compared** The benchmark compares the performance of four different iteration methods: 1. **`forEach`**: Iterates over the array using the `forEach` method, applying the common function to each element. 2. **`reduce`**: Iterates over the array using the `reduce` method, accumulating the results of the common function for all elements. 3. **`map`**: Iterates over the array using the `map` method, creating a new array with the result of applying the common function to each element. 4. **`filter`**: Iterates over the array using the `filter` method, creating a new array with only the elements for which the common function returns true. **Pros and Cons** Here's a brief overview of the pros and cons of each approach: * **`forEach`**: + Pros: Simple to implement, suitable for iterative operations. + Cons: May not be as efficient as other methods due to the need for explicit looping. * **`reduce`**: + Pros: Efficiently accumulates results, suitable for aggregating data. + Cons: Can be less readable than other methods, requires careful handling of initial values. * **`map`**: + Pros: Creates a new array with transformed data, suitable for operations that require multiple outputs. + Cons: May create unnecessary memory allocation and copying. * **`filter`**: + Pros: Efficiently removes unwanted elements, suitable for filtering data. + Cons: Can be slower than other methods due to the need for conditional checks. **Library and Purpose** In this benchmark, no external libraries are used. However, if we consider the common function `someFn(i)` as a utility library, its purpose is to perform a complex calculation on each array element. **Special JS Feature or Syntax** This benchmark does not explicitly use any special JavaScript features or syntax beyond the standard iteration methods. If you're interested in exploring more advanced techniques, you could consider using other libraries like Lodash or Ramda, which provide additional functional programming utilities. **Other Alternatives** If you'd like to explore alternative approaches for iterating over arrays, here are a few options: 1. **Using `for` loops**: A basic iterative approach that can be optimized with caching and loop unrolling. 2. **Using `Array.prototype.every()`**: An alternative filtering method that checks all elements in the array. 3. **Using `Array.prototype.some()`**: Another filtering method that checks at least one element in the array. Keep in mind that these alternatives might not offer significant performance benefits over the original methods and may introduce additional complexity. I hope this explanation helps you understand the benchmark and its associated options!
Related benchmarks:
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?