Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs reduce vs map vs filter vs for -- exlord
(version: 0)
Comparing performance of:
forEach vs reduce vs map vs filter vs for
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;
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,l=arr.length; j < l; 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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark on MeasureThat.net compares the performance of five different approaches to iterating over an array and performing a calculation: * **`forEach`:** This method loops through each element in the array and executes a provided function for each element. In this case, the function `someFn` is applied to each element of the array, and the result is added to `sumForEach`. * **`reduce`:** This method iterates over the array and accumulates a single value. It takes a function that operates on the accumulated value and the current array element. The result is the final accumulated value. Here, it's used to sum the results of `someFn` applied to each element. * **`map`:** This method creates a new array by applying a provided function to each element in the original array. The `sumMap` variable keeps track of the sum of these results. * **`filter`:** This method creates a new array containing only the elements that pass a test implemented by the provided function. In this benchmark, it's not actually filtering; it's simply summing the results of applying `someFn` to each element and storing them in `sumFilter`. * **`for` loop:** This is a traditional loop that iterates over the array using an index variable. **Pros & Cons:** * **`forEach`:** Easy to read and understand, but not ideal for tasks requiring a cumulative result. * **`reduce`:** Efficient for tasks involving aggregation (like summing or finding the average) as it iterates only once. Can be more verbose than `for`. * **`map`:** Useful for transforming arrays into new arrays with modified values. * **`filter`:** Excellent for selecting specific elements from an array based on a condition. * **`for` loop:** Gives fine-grained control over the iteration process, but can be less readable and prone to errors compared to higher-order functions. **Other Considerations:** The choice of method depends heavily on the specific task. * If you need to sum or aggregate data, `reduce` is often the most efficient choice. * For transforming arrays into new arrays, `map` is a good option. * If you need to select specific elements based on a condition, `filter` is ideal. **Alternatives:** Libraries like Lodash offer optimized versions of these array methods, potentially leading to further performance improvements. However, for simple tasks like this benchmark demonstrates, the built-in JavaScript methods are likely sufficient.
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 (slightly optimized for, fixed fn)
forEach vs reduce vs map vs filter vs for (slightly optimized for) FORK
forEach vs reduce vs map vs filter vs for (slightly optimized for (fixed))
Comments
Confirm delete:
Do you really want to delete benchmark?