Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs reduce vs map vs filter vs for 100k
(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 < 100000; 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:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0
Browser/OS:
Chrome 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
66.6 Ops/sec
reduce
68.7 Ops/sec
map
56.7 Ops/sec
filter
63.0 Ops/sec
for
72.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Measuring the performance of different JavaScript iteration methods is a crucial task, especially when it comes to optimizing code for production environments. **Benchmark Definition** The provided JSON defines a benchmark that measures the execution time of four different iteration methods: 1. `forEach` 2. `reduce` 3. `map` 4. `filter` Each method is tested on an array of 100,000 elements, where each element is a function call to the `someFn` function. The `someFn` function performs some basic arithmetic operations. **Options Compared** The four iteration methods being compared are: 1. **forEach**: Iterates over an array using the `forEach` method. 2. **reduce**: Accumulates a value by repeatedly applying an accumulator function to each element in the array. 3. **map**: Creates a new array with the results of applying an input function on every element in this array. 4. **filter**: Creates a new array with all elements that pass the test implemented by the provided function. **Pros and Cons** Here's a brief overview of the pros and cons of each iteration method: * `forEach`: * Pros: Easy to use, simple syntax, works well for most scenarios. * Cons: Can be slower than other methods due to the overhead of the loop control logic. * `reduce`: * Pros: Efficient for accumulating values, can handle large datasets efficiently. * Cons: Requires a callback function with two arguments (accumulator and current value), can be more complex to understand and use. * `map`: * Pros: Creates a new array with the results of the transformation, easy to read and maintain. * Cons: Can consume additional memory due to the creation of a new array, slower than other methods for large datasets. * `filter`: * Pros: Efficiently filters out unwanted elements from an array, easy to understand and use. * Cons: Similar to map, can be slower than other methods for large datasets. **Other Considerations** When choosing an iteration method, consider the following factors: * **Memory Usage**: If memory efficiency is crucial, `reduce` or `filter` might be a better choice due to their ability to accumulate values without creating a new array. * **Readability and Maintainability**: For complex logic, `map` or `forEach` might be more suitable due to their simplicity and readability. **Libraries and Special JS Features** In this benchmark, no libraries are used. However, if they were needed, the following libraries could potentially impact the results: * Lodash (or similar utility libraries) often provides optimized versions of these methods. * Some browsers or environments might have specific features like Web Workers or `async/await` that can affect performance. **Special JS Features** This benchmark does not utilize any special JavaScript features, such as async/await, generators, or Web Workers. If the test case were to use one of these features, it would be necessary to account for their potential impact on performance. **Alternatives** If you're looking for alternative iteration methods in JavaScript, consider: * **Closures**: Using a closure can provide more control over the iteration process. * **For Loops**: Traditional for loops without `forEach`, `map`, or `filter` might be an option for certain use cases. The provided benchmark gives a good starting point to compare and contrast different JavaScript iteration methods. By understanding their strengths, weaknesses, and implications, developers can make informed decisions about which method to use in specific scenarios.
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 vs for..of big list
forEach vs reduce vs map vs filter vs for (slightly optimized for (fixed))
Comments
Confirm delete:
Do you really want to delete benchmark?