Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs for of3
(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++; }
for of
let counter = 0; for (const obj of data) { if (obj == obj) 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 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 120 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
2.2 Ops/sec
for of
22.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and results to explain what's being tested, compared, and discussed. **Benchmark Definition Overview** The benchmark is designed to compare two approaches for iterating over an array: 1. **Traditional For Loop (`for`)**: This approach uses a traditional loop with an index variable. 2. **For...Of Loop (`for of`)**: This approach uses the "for...of" syntax, which allows iteration over arrays without needing an explicit index. **Options Compared** The benchmark compares two options: * Traditional For Loop (`for`) * For...Of Loop (`for of`) **Pros and Cons of Each Approach** 1. **Traditional For Loop (`for`)**: * Pros: + More familiar to developers, especially those with C-style programming backgrounds. + Allows for more control over iteration (e.g., skipping certain elements). * Cons: + Can be slower due to the overhead of maintaining an index variable. 2. **For...Of Loop (`for of`)**: * Pros: + Faster, as it avoids the need to manage an index variable. + More concise and expressive syntax. * Cons: + May be less familiar or less intuitive for developers without experience with this syntax. **Library Usage** The benchmark uses a JavaScript library or function that checks if two objects are equal using the `==` operator (`obj == obj`). This is likely done to ensure consistency in the test results. The specific library used is not mentioned, but it's likely a built-in JavaScript function or an external library like Lodash. **Special JS Feature/Syntax** The benchmark uses the "for...of" syntax, which is a relatively modern feature introduced in ECMAScript 2015 (ES6). This syntax allows iteration over arrays and other iterable objects without needing to specify an index variable. The `for...of` loop is designed to be more concise and expressive than traditional for loops. **Other Alternatives** If you were to rewrite this benchmark using a different approach, some alternatives could include: * Using a native JavaScript array method like `forEach()` or `map()`. * Utilizing a functional programming style with methods like `reduce()` or `filter()`. * Implementing a custom iteration mechanism using WebAssembly or another low-level technology. Keep in mind that the optimal approach may depend on the specific use case, performance requirements, and desired development experience.
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
lodash each vs for of loop
Comments
Confirm delete:
Do you really want to delete benchmark?