Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Loop Test (forEach vs for)
(version: 0)
Testing forEch and for
Comparing performance of:
for loop vs forEach loop
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
console.log('Start testing...')
Tests:
for loop
for (let i = 0; i < 100; i += 1) { console.log(i); }
forEach loop
new Array(100).fill().forEach((val, index) => console.log(index))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for loop
forEach loop
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
gemma2:9b
, generated one year ago):
This benchmark tests the performance of two different approaches to looping through an array in JavaScript: using a traditional `for` loop and using the `forEach` method. **Options Compared:** * **`for` loop:** This is the classic approach to iterating over elements in an array. It involves manually managing the index, condition, and increment. * **`forEach` loop:** This higher-order function provides a more concise way to iterate over array elements. It accepts a callback function that is executed for each element. **Pros/Cons:** | Approach | Pros | Cons | |----------|-------------------------------------------|----------------------------------------| | `for` loop | More control over the iteration process. | Can be more verbose and harder to read. | | `forEach` | More concise, easier to read | Less flexible for complex iteration needs.| **Considerations:** * **Readability:** The `forEach` approach is generally considered more readable due to its simplicity. * **Performance:** In most cases, the performance difference between `for` and `forEach` is negligible. Modern JavaScript engines often optimize both approaches efficiently. This benchmark's results show a slight advantage for `forEach` in this specific case. **Alternatives:** While not included in the benchmark, there are other ways to iterate over arrays in JavaScript: * **`for...in` loop:** Iterates over the *keys* of an object (not ideal for arrays). * **`while` loop:** Manual iteration based on a condition. Less common for array processing. **Other Notes:** This benchmark doesn't use any special libraries or JavaScript features beyond standard looping constructs.
Related benchmarks:
Regular for vs forEach
foreach vs for of
Array fill foreach, vs for i loop
for of vs forEach with console log
Comments
Confirm delete:
Do you really want to delete benchmark?