Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs for of2
(version: 0)
Comparing performance of:
for vs for of
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var count = 1000 * 1000 var data = []; do { data.push(count); } while(--count);
Tests:
for
let counter = 0; for (var index = 0; index < data.length; index++) { var obj = data[index]; if (obj == obj) counter++; } console.log(counter);
for of
let counter = 0; for (var obj of data) { if (obj == obj) counter++; } console.log(counter);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for
for of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:122.0) Gecko/20100101 Firefox/122.0
Browser/OS:
Firefox 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
587.2 Ops/sec
for of
165.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark being tested. **What is being tested?** The provided JSON represents two individual test cases for measuring the performance difference between traditional `for` loops and a newer, more modern approach called "for...of" loops in JavaScript. The tests aim to determine which loop construct is faster when iterating over an array of values and incrementing a counter variable. **Loop options compared** There are two loop options being compared: 1. **Traditional `for` loop**: This type of loop uses a counter variable (`index`) that increments manually using the `++` operator. The loop iterates over the array, and for each iteration, it checks if an object is equal to itself. 2. **For...of loop**: This newer loop construct allows iterating directly over arrays and objects without the need for a manual counter. It uses the `for...of` syntax and an iterator object (`obj`) to access each element of the array. **Pros and cons of each approach** 1. **Traditional `for` loop**: * Pros: More control over iteration, can be optimized manually for specific use cases. * Cons: Requires manual incrementing of the counter variable, which can lead to bugs if not done correctly. 2. **For...of loop**: * Pros: Simplifies iteration and reduces potential for errors, as it abstracts away manual counter management. * Cons: Can be slower due to additional overhead introduced by the iterator object. **Library usage** There is no explicit library mentioned in the provided JSON. However, if we consider the `data` array being iterated over, we can assume that a built-in JavaScript feature (or a standard library) is used for creating and manipulating arrays. **Special JavaScript features or syntax** The test case uses the "for...of" loop, which is a newer JavaScript feature introduced in ECMAScript 2015. This feature allows iterating directly over arrays and objects without the need for manual counter management. **Other alternatives** There are alternative ways to iterate over arrays in JavaScript, such as: 1. **Using `forEach()`**: While not directly comparable to traditional loops or for...of loops, `forEach()` is a built-in method that can be used to iterate over arrays. 2. **Using `map()`, `filter()`, and other array methods**: These methods are designed for more functional programming-style approaches and may have different performance characteristics compared to traditional loops. In summary, the provided benchmark aims to determine which loop construct (traditional `for` or for...of) is faster when iterating over an array of values. The results will provide insights into the performance differences between these two approaches in modern JavaScript environments.
Related benchmarks:
lodash forEach vs array.forEach vs for loop
For vs Foreach copying an array
Counting items in an array ... for vs foreach vs for of
For vs for of vs forEach
for vs foreach vs some vs for..of fixed
Comments
Confirm delete:
Do you really want to delete benchmark?