Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs reduce vs map vs filter vs for2
(version: 0)
Comparing performance of:
forEach vs reduce vs map vs filter vs for
Created:
6 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 lastValue += someFn(item); }, 0);
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:
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 compares five different ways to iterate over an array of numbers and perform a calculation on each element in JavaScript: * **forEach:** Iterates through each element of the array, executing a provided function for each element. In this case, it calls `someFn` for every item in the array and adds the result to `sumForEach`. * **reduce:** Reduces the array to a single value by iterating through it and applying a function that accumulates a result. Here, it starts with an initial value of 0 (`lastValue`) and for each element, it adds the result of `someFn` to the current `lastValue`. * **map:** Creates a new array by applying a function to each element of the original array. In this test, it applies `someFn` to each element and accumulates the results in `sumMap`, but doesn't use the new array. * **filter:** Creates a new array containing only the elements for which a provided function returns true. In this test, it applies `someFn` to each element (though filtering logic isn't used), accumulating the results in `sumFilter`. * **for loop:** A traditional loop that iterates over the array's indices and accesses each element directly, calculating the sum within the loop. **Pros & Cons:** * **forEach:** Simple and readable, good for side effects or when you need to perform actions on each element but don't need a final result. * **reduce:** Powerful for aggregating data into a single value, often used for calculations that involve accumulating a sum, product, or other value. * **map:** Efficient for creating new arrays based on transformations of existing elements, useful when you need to generate a modified version of the original array. * **filter:** Ideal for selecting specific elements from an array based on a condition, simplifying data manipulation and analysis. * **for loop:** Provides fine-grained control over iteration, allowing direct access to indices and potential for optimization in some cases. **Considerations:** * The performance differences observed in the benchmark results might be influenced by factors like JavaScript engine optimizations, the specific calculations performed within `someFn`, and other code executing concurrently on the system. * Choosing the best approach often depends on the specific use case and desired outcome. Consider the nature of the data manipulation, whether you need a final result, the complexity of the operations, and readability for maintainability. **Alternatives:** While not explicitly tested in this benchmark, other JavaScript paradigms like asynchronous programming with Promises or async/await could be used to handle array processing differently, especially when dealing with large datasets or I/O-bound operations.
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?