Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs for of4
(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 sum = 0; for (const obj of data) { sum += obj; }
for of
let sum = 0; for (let index = 0; index < data.length; index++) { sum += data[index]; }
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
56.7 Ops/sec
for of
5.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark compares two approaches for iterating over an array: traditional `for` loops versus using the `for...of` loop syntax. The test case uses JavaScript, which is used as a programming language to measure performance. **Script Preparation Code** The script preparation code sets up the benchmark by: 1. Defining a large array `data` with 1 million elements. 2. Declaring a variable `count` initialized to 0 and set to decrement every time through the loop. This code initializes the test environment, creating a scenario where both `for` loops will iterate over the same data structure. **Html Preparation Code** There is no HTML preparation code provided, which means that the benchmark does not consider factors like rendering or user interaction. **Options Being Compared** The two options being compared are: 1. **Traditional `for` loop**: This approach uses a traditional `for` loop with an incrementing counter (`count`) to iterate over the array. 2. **`for...of` loop syntax**: This approach uses the new `for...of` loop syntax, which iterates directly over the elements of an array. **Pros and Cons** Here are some pros and cons for each approach: * **Traditional `for` loop**: + Pros: Easy to understand, more control over iteration. + Cons: More verbose, can be slower due to overhead of incrementing a counter. * **`for...of` loop syntax**: + Pros: Concise, easier to read, and potentially faster since it avoids the overhead of incrementing a counter. + Cons: Less control over iteration, may have performance implications depending on the JavaScript engine. **Library Used** There is no specific library used in this benchmark. However, some JavaScript engines like V8 (used by Google Chrome) provide optimizations for `for...of` loops that can lead to better performance compared to traditional `for` loops. **Special JS Feature or Syntax** The benchmark uses the new `for...of` loop syntax, which is a relatively recent addition to JavaScript. This syntax allows iterating over arrays, objects, and other iterable data structures in a concise way. **Other Alternatives** If you're looking for alternatives to these approaches, consider: * **Array.prototype.forEach()**: A built-in method that iterates over an array without requiring explicit control over iteration. * **Iterators**: A way to create custom iterators using classes or functions, which can provide more control over iteration. Keep in mind that the choice of approach depends on the specific use case and performance requirements. The `for...of` loop syntax is often a good choice for iterating over arrays due to its concise nature, but traditional `for` loops may still be preferred when fine-grained control over iteration is necessary.
Related benchmarks:
map vs forEach vs for loop
lodash forEach vs array.forEach vs for loop
for vs foreach vs map 2
Counting items in an array ... for vs foreach vs for of
For vs for of vs forEach
Comments
Confirm delete:
Do you really want to delete benchmark?