Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for loop vs for of
(version: 0)
comparing perf between for of and for loop
Comparing performance of:
for test vs for of test
Created:
3 years ago
by:
Guest
Jump to the latest result
Tests:
for test
var arr = ['hello', 'a', 'b']; let val; for(i=0; i<arr.length; i++){ var value = arr[i]; if (value === 'b') { val = value; break; } }
for of test
var arr = ['hello', 'a', 'b']; let val; for (var value of arr) { if (value === 'b') { val = value; break; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for test
for of test
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 dive into the world of JavaScript microbenchmarks! **What is being tested?** MeasureThat.net is comparing the performance of two approaches for iterating over arrays in JavaScript: traditional `for` loops and the new `for...of` syntax. The benchmark is testing which approach is faster when searching for a specific value (`'b'`) within an array. The test case uses a fixed-size array with three elements: `'hello'`, `'a'`, and `'b'`. **Options compared** There are two options being compared: 1. **Traditional `for` loop**: This involves using a traditional `for` loop to iterate over the array, incrementing an index variable (`i`) until it reaches the length of the array. 2. **`for...of` syntax**: This is a new way of iterating over arrays introduced in ECMAScript 2015 (ES6). It uses a `for...of` loop with a `valueOf()` method to extract each element from the array. **Pros and cons** * Traditional `for` loop: + Pros: widely supported, easy to understand. + Cons: can be slower than `for...of`, more verbose. * `for...of` syntax: + Pros: more concise, potentially faster (but not always). + Cons: may have issues with array length properties or custom arrays. **Library usage** In this benchmark, there is no explicit library usage mentioned. However, the `for...of` syntax relies on the built-in `Array.prototype.forEach()` method, which is a part of the ECMAScript standard and therefore not considered a external library. **Special JS features or syntax** The benchmark uses the new `for...of` syntax, which was introduced in ES6. This means that users who are familiar with this feature will be able to run this benchmark easily. **Other alternatives** If you're interested in running alternative benchmarks, here are some common options: * **Loops**: You can create your own loop-based benchmark using a traditional `for` loop or a while loop. * **Array methods**: Some array methods like `indexOf()`, `findIndex()`, or `every()` might be faster for certain use cases. * **Native functions**: Built-in functions like `map()`, `filter()`, or `reduce()` can provide different performance characteristics. Keep in mind that benchmarking JavaScript performance is a complex topic, and the results may vary depending on the specific use case, platform, and version of JavaScript being used.
Related benchmarks:
foreach vs for vs for in
foreach vs for..of
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
Math.pow vs ** vs * vs for loop
Comments
Confirm delete:
Do you really want to delete benchmark?