Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs for of5
(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 (let index = 0; index < data.length; index++) { sum += data[index]; }
for of
let sum = 0; for (const obj of data) { sum += obj; }
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:121.0) Gecko/20100101 Firefox/121.0
Browser/OS:
Firefox 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
249.6 Ops/sec
for of
97.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases to understand what is being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark measures the performance difference between two loop constructs in JavaScript: traditional `for` loops versus the more modern and concise `for...of` loop syntax. The benchmark creates an array of 1 million elements, initializes a variable `sum` to zero, and then uses each loop construct to iterate through the array, adding up the values. **Script Preparation Code** The script preparation code is identical for both loops: ```javascript var count = 1000 * 1000; var data = []; do { data.push(count--); } while (count); ``` This code creates an array `data` with a million elements, each initialized to the value of `count`, which starts at 1 million and decrements by 1 in each iteration. The `for` loop uses a traditional `do-while` loop syntax, while the `for...of` loop uses a more modern syntax. **Html Preparation Code** The HTML preparation code is empty for both loops, indicating that the benchmark only measures JavaScript performance and does not consider other factors like browser rendering or network latency. **Test Cases** There are two test cases: 1. **"for"`** ```javascript let sum = 0; for (let index = 0; index < data.length; index++) { sum += data[index]; } ``` This loop uses a traditional `for` loop with an indexed variable `index`. 2. **"for of"`** ```javascript let sum = 0; for (const obj of data) { sum += obj; } ``` This loop uses the more modern `for...of` loop syntax, which iterates over the elements of an array without an explicit index. **Pros and Cons** * **Traditional `for` loops**: Pros: + Wide support across older browsers + Can be easier to understand for developers familiar with traditional loop syntax + Often faster due to less overhead from the modern loop syntax * **Modern `for...of` loops**: Pros: + More concise and expressive code + Reduced chance of off-by-one errors + Better support for array iteration in modern browsers However, traditional `for` loops might be slightly faster and more efficient due to less overhead from the modern loop syntax. **Library Usage** There is no explicit library usage mentioned in the benchmark definition. However, it's worth noting that some libraries like Lodash or Ramda might provide optimized implementation of array iteration, which could potentially affect the benchmark results. **Special JS Feature/ Syntax** The `for...of` loop uses a special JavaScript feature: **Array Iteration Protocol (AIP)**. This protocol defines how arrays should be iterated over, and modern browsers follow this protocol for better compatibility and performance. The `for...of` loop syntax leverages this AIP to iterate over the elements of an array without requiring an explicit index. **Alternatives** Some alternative loop constructs that could be used in a benchmarking scenario include: * **While loops**: Can be used as an alternative to traditional `for` loops. * **For-in loops**: Can be used to iterate over object properties, but might not be suitable for array iteration due to differences in behavior between objects and arrays. * **Generator functions**: Could potentially be used as a more modern loop construct, but would likely require additional setup and optimization. Keep in mind that the choice of loop construct depends on the specific requirements and constraints of the benchmarking scenario.
Related benchmarks:
lodash forEach vs array.forEach vs for loop
Array loop vs foreach vs map vs for of
lodash forEach vs for i loop yo
For vs Foreach copying an array
Counting items in an array ... for vs foreach vs for of
Comments
Confirm delete:
Do you really want to delete benchmark?