Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for of loop
(version: 0)
for item of array v.s. for n=0;n<array.length;++n
Comparing performance of:
for n vs for of ..
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var testArray = Array(10000);
Tests:
for n
const l = testArray.length; for ( let n=0; n < l; ++n) if ( testArray[n] === "$" ) continue;
for of ..
for ( const n of testArray) if ( n === "$" ) continue;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for n
for of ..
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):
Let's break down the benchmark definition and test cases. **Benchmark Definition** The benchmark is designed to compare two approaches for iterating over an array: traditional `for` loop with incrementing index (`n`) versus the newer `for...of` loop syntax. **Script Preparation Code** Before running each test case, a script prepares the environment: ```javascript var testArray = Array(10000); ``` This creates an array of 10,000 elements and assigns it to the `testArray` variable. **Html Preparation Code** There is no HTML preparation code provided, which means that this benchmark only measures JavaScript execution performance and does not account for any overhead from rendering or displaying HTML content. **Test Cases** There are two individual test cases: 1. **"for n"`** This test case uses a traditional `for` loop with incrementing index (`n`) to iterate over the array: ```javascript for (let n = 0; n < l; ++n) { if (testArray[n] === "$") continue; } ``` In this code, `l` is the length of the `testArray`, which was calculated earlier. The loop iterates from index 0 to the length of the array, incrementing `n` by 1 on each iteration. 2. **"for of .."`** This test case uses the new `for...of` loop syntax to iterate over the array: ```javascript for (const n of testArray) { if (n === "$") continue; } ``` In this code, the `for...of` loop iterates directly over the elements of the `testArray`, assigning each element to the variable `n`. The rest of the code is similar to the traditional `for` loop. **Library and Features** There are no external libraries used in these test cases. However, it's worth noting that the `for...of` loop syntax was introduced in ECMAScript 2015 (ES6) and provides a more concise way to iterate over arrays and other iterable objects. **Browser and Device Information** The latest benchmark results show two browsers running on different devices: 1. **Firefox 117**: This browser is running on a desktop Linux system, with an execution rate of approximately 45249 executions per second. 2. **Firefox 117**: This same browser is running on the same device, but with a different test case ("for of .."), resulting in an execution rate of approximately 9876 executions per second. **Pros and Cons** Here's a brief analysis of the pros and cons of each approach: * Traditional `for` loop: + Pros: more control over iteration, easier to understand for developers familiar with older JavaScript versions. + Cons: can be less efficient than `for...of`, requires manual index calculation and incrementing. * `for...of` loop: + Pros: concise, expressive syntax, reduces chance of errors related to index management. + Cons: may require more time to understand for developers unfamiliar with the new syntax. **Other Alternatives** If you're interested in exploring alternative benchmarking scenarios or test cases, consider these: * Using different data structures (e.g., objects, sets, maps) * Incorporating additional libraries or frameworks * Experimenting with different iteration patterns (e.g., while loops, recursive functions) These variations can help you gain a deeper understanding of the performance characteristics of each approach and identify potential optimization opportunities in your own code.
Related benchmarks:
for vs forEach
for vs forEach
for vs forEach
teststest
Comments
Confirm delete:
Do you really want to delete benchmark?