Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs reduce vs map vs filter vs for vs for of
(version: 0)
Comparing performance of:
forEach vs reduce vs map vs filter vs for vs forOf
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, sumForOf = 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 += someFn(arr[j]); }
forOf
for (const j of arr) { sumForOf += someFn(arr[j]); }
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'm ready to break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark measures the performance of six different JavaScript iteration methods: 1. `forEach` 2. `reduce` 3. `map` 4. `filter` 5. `for` loop 6. `for...of` loop Each method is used to iterate over an array and calculate a value. **Library Usage** One library is used in this benchmark: Lodash's `_` function, which is not explicitly mentioned in the provided code. However, based on the context, it's likely that the `_` function from Lodash is being used as a utility function to provide `someFn(item)`. **Special JavaScript Features/Syntax** There are no special JavaScript features or syntax being tested in this benchmark. **Comparison of Iteration Methods** Here's a brief overview of each iteration method and their pros/cons: 1. **`forEach`**: Uses the `Array.prototype.forEach()` method, which iterates over an array using a callback function. * Pros: Simple to use, easy to read. * Cons: Can be less efficient than other methods because it uses a separate loop variable. 2. **`reduce`**: Uses the `Array.prototype.reduce()` method, which applies a reduction operation to each element in the array. * Pros: Efficient and concise, can perform multiple operations on the same data. * Cons: Can be less readable for complex logic, may require additional setup. 3. **`map`**: Uses the `Array.prototype.map()` method, which creates a new array with transformed values. * Pros: Easy to use, produces a new array with transformed values. * Cons: Creates an additional array object, can consume more memory. 4. **`filter`**: Uses the `Array.prototype.filter()` method, which creates a new array with filtered elements. * Pros: Efficient and concise, filters out unwanted data. * Cons: Creates an additional array object, can consume more memory. 5. **`for` loop**: Iterates over the array using a traditional `for` loop, incrementing a counter variable. * Pros: Low-level control, no dependency on browser or runtime features. * Cons: Can be error-prone, requires manual index management. 6. **`for...of` loop**: Iterates over the array using an enhanced `for` loop, which uses the iterator protocol. * Pros: Easy to use, concise, and readable. * Cons: Limited control over iteration flow. **Other Alternatives** Some alternative iteration methods not included in this benchmark include: 1. `every()`, `some()`: These methods can be used for filtering or testing conditions on individual elements. 2. `Array.prototype.every()`/`Array.prototype.some()`: These methods can be used to test conditions on all elements in the array. 3. Callback functions with manual loop management: This approach requires manually managing an index variable and looping through the array. Overall, this benchmark provides a good starting point for understanding the performance differences between various iteration methods in JavaScript.
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?