Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs forEach & filter
(version: 0)
Comparing performance of:
forEach vs forEach with filter
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]; var sum = 0 var isEven = (num) => num % 2 === 0
Tests:
forEach
arr.forEach((item) => { if(isEven(item)) { sum += item; } });
forEach with filter
arr.filter(isEven).forEach((item) => sum += item);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
forEach
forEach with filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
706663.1 Ops/sec
forEach with filter
1017227.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its results. **Benchmark Purpose** The benchmark is designed to compare the performance of two approaches: 1. **forEach** (without using `filter`): This approach iterates over an array using the `forEach` method, which calls a callback function for each element in the array. 2. **forEach with filter**: This approach uses the `filter` method to first create a new array containing only the even numbers from the original array, and then iterates over this filtered array using the `forEach` method. **Options Compared** The benchmark compares the performance of these two approaches: * **No optimization**: The first approach ( `forEach` ) is used without any optimizations. * **Optimized filter usage**: The second approach ( `forEach with filter` ) uses the `filter` method to create a new array, which can lead to additional overhead due to memory allocation and garbage collection. **Pros and Cons** 1. **No optimization (forEach)**: * Pros: Simple and straightforward implementation. * Cons: May have additional overhead due to unnecessary iterations or allocations. 2. **Optimized filter usage (forEach with filter)**: * Pros: Can reduce the number of iterations and memory allocations, leading to better performance. * Cons: Introduces additional complexity and potential overhead due to the `filter` method. **Library Usage** The benchmark uses the following library: * None explicitly mentioned. However, it's likely that JavaScript engines use various internal libraries and optimizations under the hood. **Special JS Features or Syntax** None are used in this specific benchmark. The focus is on comparing two straightforward implementations of the `forEach` method. **Other Alternatives** If you were to rewrite this benchmark, you could consider alternative approaches: * Using a different iteration method, such as `for...of` loops or recursive functions. * Utilizing native array methods like `reduce`, `map`, or `every`. * Implementing a custom iterator or using WebAssembly for better performance. Some potential variations could include: * Comparing the performance of using `forEach` with and without caching the results. * Using different data structures, such as linked lists instead of arrays. * Incorporating additional optimizations, like memoization or parallel processing.
Related benchmarks:
for..of vs foreach
for vs foreach vs map vs (map, filter reduce)
for of vs Array.reduce vs Array.forEach vs for i for summing and array of integers
for of vs forEach with add operation
Array.prototype.reduce() vs for loop sum
Comments
Confirm delete:
Do you really want to delete benchmark?