Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
foreach vs for of
(version: 0)
testing which one is the fastest
Comparing performance of:
testing for of vs Testing native foreach
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(1000) for(let i = 0; i < array.length; i++) { array[i] = array.length - i }
Tests:
testing for of
for(const element of array) { console.log(element) }
Testing native foreach
array.forEach(element => { console.log(element) })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
testing for of
Testing native foreach
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 break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The provided benchmark compares two approaches: `for...of` loop and the native `forEach()` method in JavaScript. The goal is to determine which one is faster. **Script Preparation Code** The script preparation code creates an array of 1000 elements and initializes them with values from `array.length - i`. This is done using a traditional `for` loop, where `i` is incremented on each iteration. ```javascript var array = new Array(1000); for (let i = 0; i < array.length; i++) { array[i] = array.length - i; } ``` **Html Preparation Code** There is no HTML preparation code provided, which means that the benchmark focuses solely on the JavaScript execution speed. **Individual Test Cases** There are two test cases: 1. **Testing native foreach** ```javascript for (const element of array) { console.log(element); } ``` This test case uses a `for...of` loop to iterate over the array and logs each element to the console. 2. **Testing for of** ```javascript array.forEach(element => { console.log(element) }) ``` This test case uses the native `forEach()` method on the array, passing an arrow function that takes one argument (`element`) and logs it to the console. **Library** Neither of these test cases relies on any external libraries. They only use built-in JavaScript features. **Special JS Feature or Syntax** The `for...of` loop and the native `forEach()` method are special syntaxes introduced in ECMAScript 2015 (ES6). The `for...of` loop is designed for iterating over arrays, objects, and iterables, while the `forEach()` method provides a convenient way to perform an action on each element of an array. **Pros and Cons** **For...of Loop:** Pros: * More concise and readable code * Easier to write and maintain * No need to declare the length of the array Cons: * Requires ECMAScript 2015 (ES6) support * May be slower than traditional `for` loops in some cases **Native forEach():** Pros: * Faster execution speed compared to `for...of` * Works with any iterable, not just arrays * More widely supported across browsers and environments Cons: * Less concise and less readable code * Requires explicit callback function **Other Considerations** When choosing between these two approaches, consider the following factors: * Performance: If you need optimal performance, `forEach()` might be a better choice. * Readability and Maintainability: `for...of` loops are generally more readable and maintainable. In this specific benchmark, the results show that the native `forEach()` method outperforms the `for...of` loop. However, it's essential to note that this result may vary depending on the specific use case and environment.
Related benchmarks:
Array fill foreach, vs for i loop
foreach vs for..of
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
Array fill vs for i loop
Comments
Confirm delete:
Do you really want to delete benchmark?