Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test for vs foreach vs forof
(version: 0)
Comparing performance of:
for of vs foreach vs for
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const COUNT = 10000; var data = []; for (let i = 0; i < COUNT; i++) { data[i] = i; }
Tests:
for of
let i = 0; for (const item of data) { i++; } console.log(i);
foreach
let i = 0; data.forEach((item) => { i++; }); console.log(i);
for
let j = 0; let len = data.length; for (let i = 0; i < len; i++) { j++; } console.log(j);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for of
foreach
for
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):
Let's break down the provided JSON and explain what's being tested, along with the pros and cons of each approach. **Benchmark Overview** The benchmark is designed to compare the performance of three different loop constructs in JavaScript: 1. `for...of` 2. `forEach` (which is actually a method on arrays) 3. A traditional `for` loop **Script Preparation Code** The script preparation code creates an array `data` with 10,000 elements and initializes it with incremental values from 0 to 9,999. **Html Preparation Code** There is no HTML preparation code provided, which means the benchmark only tests the JavaScript loops in isolation, without any additional overhead or distractions. **Individual Test Cases** Each test case is a simple loop that increments a counter variable. The differences between the three loops are: * `for...of`: Iterates over an array using the `for...of` statement, which implicitly converts the array to an iterator. * `forEach`: Iterates over an array using the `forEach` method, which expects a callback function as its argument. * Traditional `for` loop: Manually increments a counter variable using a traditional loop syntax. **Library Used** In this benchmark, the library used is JavaScript itself. There are no external libraries or frameworks involved. **Special JavaScript Features/Syntax** None of the test cases use any special JavaScript features or syntax beyond the standard `for...of` and `forEach` constructs. **Options Compared** The three loops are compared in terms of their performance, measured by the number of executions per second. The test aims to determine which loop construct is fastest. **Pros and Cons of Each Approach** Here's a brief analysis of each approach: * `for...of`: + Pros: Convenient and expressive syntax, works with arrays, etc. + Cons: May not be as efficient as traditional loops in some cases (e.g., when iteration order matters). * `forEach`: + Pros: Easy to read and write, convenient for array iteration. + Cons: May have performance overhead due to the additional function call, especially in older browsers. * Traditional `for` loop: + Pros: Fastest and most control over iteration. + Cons: More verbose syntax and requires manual index management. **Other Alternatives** If you're interested in exploring other alternatives, here are a few options: * `while` loops * `do...while` loops * Closures (for more complex control flow) * Iterators and generators (for even more flexible control) Keep in mind that the choice of loop construct depends on the specific use case and performance requirements. **Benchmark Results** The latest benchmark results show that: 1. The traditional `for` loop is the fastest, followed closely by the `forEach` method. 2. The `for...of` loop is slower than both previous options. These results may vary depending on the specific browser, JavaScript engine, and system configuration being used. I hope this explanation helps!
Related benchmarks:
for vs foreach vs map 2
Get index with forEach vs for...of over entries
for vs foreach vs some vs for..of fixed
For loop vs <Array>.forEach() vs for...of loop
forEach vs for of 7
Comments
Confirm delete:
Do you really want to delete benchmark?