Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for of vs for loop v2
(version: 0)
Comparing performance of:
for of vs for loop
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [...Array(1000).keys()]
Tests:
for of
let z = []; for (const num of arr) { if(num % 3 || num % 5) { z.push(num); } }
for loop
let z = []; for (let x = 0; x < arr.length; x++) { if(arr[x] % 3 || arr[x] % 5) { z.push(arr[x]); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for of
for 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
llama3.2:3b
, generated one year ago):
I'd be happy to explain the JavaScript benchmark you provided. **What is tested:** The benchmark compares two approaches for iterating over an array in JavaScript: 1. **For...of loop**: This is a new syntax introduced in ECMAScript 2015 (ES6) that allows iteration over arrays and other iterable objects. 2. **Traditional For Loop v2**: This is the older way of looping through an array, which uses a variable to keep track of the current index. **Options compared:** The benchmark compares these two options: * **For...of loop**: This option uses the new for...of loop syntax to iterate over the array. * **Traditional For Loop v2**: This option uses a traditional for loop with an incrementing index variable (x). **Pros and Cons of each approach:** **For...of loop:** Pros: * More concise and readable code * Less chance of off-by-one errors * Automatic handling of array iteration limits Cons: * Limited control over the iteration process * Not supported in older JavaScript versions (pre-ES6) **Traditional For Loop v2:** Pros: * More control over the iteration process * Supported in all JavaScript versions Cons: * Longer code and more prone to off-by-one errors * Requires manual handling of array iteration limits **Other considerations:** * The benchmark measures the **Executions Per Second**, which indicates how many iterations the loop can perform per second on a given device. * The benchmark uses **Firefox 91** as the test browser, which is a relatively recent version. **Library and special JS feature:** Neither of the options in this benchmark uses any external libraries. However, it does utilize a JavaScript feature introduced in ES6: the for...of loop syntax. **Test case explanation:** The two test cases are identical except for the iteration method used: 1. **For...of loop**: This code iterates over the array using the for...of loop syntax and pushes elements to an array (z) if they satisfy a condition. 2. **Traditional For Loop v2**: This code uses a traditional for loop with an incrementing index variable (x) and also pushes elements to an array (z) if they satisfy a condition. **Alternatives:** Other alternatives for iterating over arrays in JavaScript include: * Using the `Array.prototype.forEach()` method * Using the `Array.prototype.map()` or `Array.prototype.filter()` methods * Using a `while` loop or other manual iteration approach Each of these alternatives has its own trade-offs and use cases, but they all provide different levels of convenience, control, and performance compared to the for...of loop and traditional For Loop v2 options.
Related benchmarks:
For in vs For of
for.. of vs forEach
Array.forEach vs Object.keys().forEach
for of vs forEach with console log
For in vs Object.*.forEach vs Object.values vs _.forEach(_.values vs n=arr.length
Comments
Confirm delete:
Do you really want to delete benchmark?