Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Side effect: for i vs for of vs foreach fix
(version: 0)
Comparing performance of:
for i vs for..of vs foreach
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(65535).fill(0); var q = 0;
Tests:
for i
for (var i = 0; i < array.length; i++) { q = array[i]; }
for..of
for (var i of array) { q = i; }
foreach
array.forEach((i) => q = i);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for i
for..of
foreach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for i
182.7 Ops/sec
for..of
475.9 Ops/sec
foreach
477.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark, titled "Side effect: for i vs for of vs foreach fix", tests the performance difference between three approaches to iterate over an array: 1. **Traditional `for` loop**: This is the classic way to iterate over an array in JavaScript. The loop variable `i` is declared outside the loop, and it increments manually. 2. **`of` loop**: Introduced in ECMAScript 2015 (ES6), this loop allows iterating directly over an array's elements without declaring a separate loop variable. 3. **`forEach` method**: This is a built-in method of arrays that executes a callback function for each element, skipping the need to declare a loop variable. **Options Compared** The benchmark compares the performance of these three approaches: * Traditional `for` loop * `of` loop * `forEach` method **Pros and Cons of Each Approach** 1. **Traditional `for` loop**: * Pros: Control over indexing, potentially more efficient for large arrays. * Cons: Requires manual incrementation, can lead to off-by-one errors if not handled correctly. 2. **`of` loop**: * Pros: More concise and readable syntax, eliminates the need for manual indexing. * Cons: Can be slower due to the overhead of creating an iterator object, and may not perform well on very large arrays. 3. **`forEach` method**: * Pros: Most concise and expressive way to iterate over an array, with built-in support for callback functions. * Cons: May have performance penalties compared to traditional `for` loops or `of` loops. **Library Usage** In the provided benchmark, no external libraries are used. However, if a library like Lodash (`_`) were used in one of the test cases, it would likely be used for its utility functions, such as array iteration helpers. **Special JS Features or Syntax** None are mentioned in this benchmark. However, it's worth noting that other benchmarks may use special features like `Set` operations, regular expressions, or async/await to test specific JavaScript capabilities. **Other Alternatives** If you were to write a similar benchmark, you might consider adding additional iterations, variations in array size or complexity, or even testing with different browsers or platforms to further exhaust the performance differences between these approaches.
Related benchmarks:
Loop Test (forEach vs for)
Array fill foreach, vs for i loop
Side effect: for i vs for of vs foreach
Array fill map, vs for i loop
Comments
Confirm delete:
Do you really want to delete benchmark?