Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs reduce vs map vs filter vs for add code
(version: 0)
Comparing performance of:
forEach vs filter
Created:
2 years ago
by:
Registered User
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; newArray = [];
Tests:
forEach
arr.forEach(item => { sumForEach += someFn(item); if(item % 2 === 0) { newArray.push(item) } });
filter
arr.filter(item => { if(item % 2 === 0) { newArray.push(item) } return sumForEach += someFn(item); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
forEach
filter
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0
Browser/OS:
Firefox 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
1905.3 Ops/sec
filter
1840.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark on MeasureThat.net, which compares the performance of different approaches for iterating over an array and performing calculations. **Script Preparation Code** The script preparation code initializes an array `arr` with 12345 elements, each containing an incrementing integer value. It also defines a function `someFn(i)` that takes an integer `i` as input and performs a series of arithmetic operations on it to produce a result. **Benchmark Definition JSON** The benchmark definition JSON consists of two test cases: 1. **forEach**: Iterates over the array using the `forEach` method, applying the `someFn` function to each element and accumulating the results in variables `sumForEach`, `newArray`. 2. **filter**: Iterates over the array using the `filter` method, applying a conditional expression that checks if the element is even and pushes it into an array (`newArray`) only if it's true. The same arithmetic operations are applied to each element as in the `forEach` case. **Comparison of Approaches** The benchmark compares the performance of four approaches: 1. **forEach**: Iterates over the array using the `forEach` method, applying a callback function to each element. 2. **filter**: Iterates over the array using the `filter` method, applying a conditional expression to each element and pushing it into an array if true. 3. **map**: Iterates over the array using the `map` method, applying a callback function to each element (not used in this benchmark). 4. **for** (loop): Iterates over the array using a traditional `for` loop. **Pros and Cons of Each Approach** * **forEach**: + Pros: Concise code, easy to read, and maintain. + Cons: May be slower due to overhead from the `forEach` method call. * **filter**: + Pros: Can be faster than `forEach` for conditional expressions with a large number of elements, as it avoids unnecessary iterations. + Cons: Requires an additional array creation, which can be memory-intensive for large datasets. * **for** (loop): + Pros: Generally the fastest approach, as it avoids method call overhead and uses direct iteration. + Cons: Can be verbose and harder to read/maintain. **Library Used** None explicitly mentioned in the benchmark definition JSON. However, some browsers may use polyfills or built-in functions that might rely on external libraries. **Special JS Feature/Syntax** None mentioned in this explanation. **Other Alternatives** * **reduce**: Similar to `forEach`, but accumulates a single value instead of an array. * **every**, **some**, and **includes** methods could also be used for similar iterations, but are not explicitly compared in the benchmark. In conclusion, this benchmark compares the performance of different approaches for iterating over an array and performing calculations, providing insights into the trade-offs between conciseness, readability, and execution speed.
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?