Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for of vs forEach vs reduce
(version: 0)
Comparing performance of:
for of vs forEach vs reduce
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var base = new Map(Array.from({ length: 100000 }).map((_, i) => [i, i]));
Tests:
for of
const map = new Map(); for (const [k, v] of base) { map.set(k, v); }
forEach
const map = new Map(); base.forEach((k, v) => map.set(k, v));
reduce
const map = Array.from(base).reduce((map, [k, v]) => { map.set(k, v); return map; }, new Map());
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for of
forEach
reduce
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):
**Benchmark Overview** The provided benchmark measures the performance of three JavaScript loop constructs: `for`-of, `forEach`, and `reduce`. The test creates a large Map object (`base`) with 100,000 key-value pairs and then iterates over it using each of these loops, appending values to a new Map object (`map`). **Loop Constructs Compared** 1. **`for`-of**: This loop constructs iterate directly over the iterable (in this case, `base`). The syntax is: ```javascript for (const [k, v] of base) { // ... } ``` **Pros:** - More explicit and readable than `forEach`. - Can be more efficient for certain use cases. **Cons:** - Less common in modern JavaScript code. - May require more setup for certain types of iterables (e.g., arrays with a custom `values()` method). 2. **`forEach`:** This loop construct executes a callback function once for each element in the iterable (in this case, `base`). The syntax is: ```javascript base.forEach((k, v) => map.set(k, v)); ``` **Pros:** - More widely used and familiar than `for`-of. - Easier to implement for certain types of iterables. **Cons:** - Less explicit and less readable than `for`-of. - Can be less efficient in some cases due to callback function overhead. 3. **`reduce`:** This loop construct applies a reduction function to each element in the iterable (in this case, `base`). The syntax is: ```javascript Array.from(base).reduce((map, [k, v]) => { // ... }, new Map()); ``` **Pros:** - More functional programming style and concise. - Can be more efficient for certain use cases. **Cons:** - Less common in modern JavaScript code. - Requires creating an initial value (in this case, `new Map()`). **Library Used** In this benchmark, no external library is used beyond the built-in `Map` object. However, it's worth noting that if you were to use a library like Lodash or Ramda, your implementation might differ. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark beyond what is inherent to each loop construct. **Other Alternatives** Some other loop constructs or methods for iterating over iterables include: - `while` loops with an explicit counter - Using `Array.prototype.forEach()` with a callback function and a separate variable for the result - Implementing your own iterator using a custom `next()` method However, these alternatives are not typically used in modern JavaScript code due to their less expressive nature or higher implementation complexity. **Benchmark Result Interpretation** The benchmark results show that, on this particular test case, `forEach` is the fastest, followed by `for`-of and then `reduce`. This ordering may vary depending on the specific use case and system characteristics.
Related benchmarks:
map vs forEach Chris
map vs forEach Chris v2
map vs forEach Chris v2b
for vs foreach vs map 2
Comments
Confirm delete:
Do you really want to delete benchmark?