Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for of vs forEach perf
(version: 0)
Comparing performance of:
forEach vs for of
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = new Array(100)
Tests:
forEach
let s = 0 arr.forEach((x) => { if (x % 2) { s += x } else { s -= x } })
for of
let s = 0 for (const x of arr) { if (x % 2) { s += x } else { s -= x } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
forEach
for of
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'll explain what's being tested in the provided JSON, describe the options compared, and discuss their pros and cons. **Benchmark Test** The benchmark tests two JavaScript loops for iterating over an array: `for...of` and `forEach`. The test aims to measure which loop is more efficient in terms of execution speed. **Options Compared** 1. **for...of**: This loop iterates over the array using the `for...of` syntax, which creates a new iterator object from the array. 2. **forEach**: This method iterates over the array by invoking it with each element as an argument, without creating a new iterator object. **Pros and Cons** 1. **for...of**: * Pros: Can be more memory-efficient since it doesn't create a new iterator object, which can lead to better performance in large datasets. * Cons: May not be compatible with older browsers or environments that don't support `for...of`. 2. **forEach**: * Pros: Wide compatibility across browsers and environments, as it's a standard method on arrays. * Cons: Creates a new iterator object, which can lead to increased memory usage in large datasets. **Library** None is explicitly mentioned in the provided JSON. However, if you're using a library like Lodash, `forEach` might be used with additional functionality or optimizations that are not directly comparable to the basic `for...of` loop. **Special JS Feature/Syntax** There's no special JavaScript feature or syntax being tested here. The focus is solely on comparing two basic iteration mechanisms: `for...of` and `forEach`. **Alternative Approaches** If you need to measure performance in a different way, consider the following alternatives: 1. **while loop**: Iterate over the array using a `while` loop instead of either `for...of` or `forEach`. 2. **Array.prototype.forEach.call()**: Use the `forEach` method with `call()` to invoke it on an array object. 3. **Custom iteration functions**: Implement your own custom iteration function using indices, manual iteration, or other techniques. Keep in mind that each alternative approach might have different trade-offs and implications for performance, compatibility, and code readability.
Related benchmarks:
Iteration through array; of vs forEach
for (i < n) vs forEach vs for...of
foreach vs for...of
for of vs forEach with console log
forEach vs for of 7
Comments
Confirm delete:
Do you really want to delete benchmark?