Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
assphinxtersayswhasff
(version: 0)
Comparing performance of:
forEach vs reduce vs map vs filter vs for vs forOf
Created:
2 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 sum = 0;
Tests:
forEach
arr.forEach(item => sum += someFn(item));
reduce
sumReduce = arr.reduce((lastValue, item) => { return sum += someFn(item); });
map
arr.map(item => (sum += someFn(item)));
filter
arr.filter(item => (sum += someFn(item)));
for
for (var j = 0; j < arr.length; j++) { sum += someFn(arr[j]); }
forOf
for (const num of arr) { sum += someFn(num); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
forEach
reduce
map
filter
for
forOf
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
381.0 Ops/sec
reduce
394.0 Ops/sec
map
340.8 Ops/sec
filter
383.7 Ops/sec
for
211.8 Ops/sec
forOf
411.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark that tests various looping constructs in JavaScript: * `forEach` * `reduce` * `map` * `filter` * `for` (traditional loop) * `forOf` (iteration with `of` keyword) **Script Preparation Code** The script preparation code creates an array `arr` with 12345 elements, each initialized with its index `i`. It also defines a function `someFn(i)` that performs some arithmetic calculation on the input `i`. **Html Preparation Code** There is no HTML preparation code provided. **Looping Constructs Comparison** Let's examine the looping constructs being compared: 1. **`forEach`**: Iterates over the array using the `forEach` method, executing the callback function for each element. * Pros: Efficient and concise way to iterate over arrays, built-in support in most browsers. * Cons: May not be suitable for use cases where iteration is controlled or optimized. 2. **`reduce`**: Iterates over the array using the `reduce` method, accumulating a result by applying a callback function to each element. * Pros: Can be useful when building complex data structures or aggregating values. * Cons: May not be suitable for simple iteration tasks due to its cumulative nature. 3. **`map`**: Iterates over the array using the `map` method, creating a new array with transformed elements. * Pros: Suitable for transforming arrays without changing their original structure. * Cons: Can be memory-intensive if dealing with large datasets. 4. **`filter`**: Iterates over the array using the `filter` method, creating a new array with filtered elements. * Pros: Useful when removing unwanted elements from an array. * Cons: Similar to `map`, can be memory-intensive for large datasets. 5. **`for` (traditional loop)**: Loops through the array using a traditional `for` loop, accessing each element directly. * Pros: Low-level control over iteration and indexing. * Cons: Can be verbose and error-prone due to manual indexing and looping logic. 6. **`forOf`**: Iterates over the array using the `for...of` loop, which provides automatic indexing and iteration. * Pros: Easy-to-use syntax and automatic indexing make it more concise than traditional loops. * Cons: May have performance overhead or limitations compared to other methods. **Library Usage** The benchmark uses the built-in JavaScript methods (`forEach`, `map`, `filter`, `reduce`) without any additional libraries. However, it's worth noting that modern browsers support these methods, but older browsers might require polyfills or alternative implementations. **Special JS Features/Syntax** There is no special JS feature or syntax used in this benchmark. The focus is on comparing the performance of various looping constructs. **Alternative Approaches** Other approaches to iterate over arrays could include: * Using a `while` loop and manual indexing. * Utilizing `Array.prototype.every`, `Array.prototype.some`, or other array methods that provide more control over iteration. * Implementing custom iteration logic using callbacks, promises, or async/await. Keep in mind that the choice of looping construct depends on specific use cases, performance requirements, and personal preference.
Related benchmarks:
sum range
forEach vs reduce vs map vs filter vs for vs lodash
filter map vs reduce
Javascript loop
Comments
Confirm delete:
Do you really want to delete benchmark?