Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
loopings
(version: 0)
Comparing performance of:
for of vs for in vs for vs while vs forEach() with function vs forEach() with closure vs for, reversed vs for, cached length vs while, reversed vs while, cached length
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script> var arr = new Array(300); var out = new Array(300); </script>
Script Preparation code:
arr = new Array(300).fill(Math.random()); out = new Array(300);
Tests:
for of
for (const item of arr) { out.push(item); }
for in
for (const i in arr) { out.push(arr[i]); }
for
for (var i = 0; i < arr.length; ++i) { out.push(arr[i]); }
while
var i = 0; while (i < arr.length) { out.push(arr[i]); i++; }
forEach() with function
arr.forEach(function(x) { out.push(x); });
forEach() with closure
arr.forEach(x => out.push(x));
for, reversed
for (var i = arr.length; i--;) { out.push(arr[i]); }
for, cached length
for (var i = 0, len = arr.length; i < len; ++i) { out.push(arr[i]); }
while, reversed
var i = arr.length; while (i-- > 0) { arr[i]; }
while, cached length
var i = 0; var len = arr.length; while (i < len) { out.push(arr[i]); i++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (10)
Previous results
Fork
Test case name
Result
for of
for in
for
while
forEach() with function
forEach() with closure
for, reversed
for, cached length
while, reversed
while, cached length
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):
Measuring the performance of different JavaScript iteration techniques is crucial for optimizing code efficiency. **Benchmark Overview** The provided JSON defines two benchmark categories: script preparation and HTML preparation. Script preparation code is executed by the browser to initialize variables, while HTML preparation code is used to set up the DOM environment. The individual test cases compare eight different iteration techniques: 1. `for of` 2. `for in` 3. `for` (traditional) 4. `while` loop 5. `forEach()` with a function callback 6. `forEach()` with an arrow function closure 7. `for`, reversed 8. `for`, cached length **Iteration Techniques Comparison** Each iteration technique has its pros and cons: * **For of**: This is the most modern and efficient way to iterate over arrays. It's fast, concise, and readable. * Pros: Efficient, concise, and readable. * Cons: Limited support for older browsers (e.g., Internet Explorer). * **For in**: This technique is used when you need to access both the index and value of each element in an array. However, it's generally slower than `for of` or traditional `for` loops. * Pros: Allows access to both index and value. * Cons: Generally slower than other techniques. * **Traditional for**: This is a straightforward way to iterate over arrays using indices. * Pros: Widely supported, efficient. * Cons: Can be error-prone if not used carefully (e.g., off-by-one errors). * **While loop**: Loops continue as long as a condition is met. It can be used for arrays or any other iterable data structure. * Pros: Flexible, can handle complex logic. * Cons: Generally slower than array-specific iteration techniques and may lead to more complex code. * **ForEach() with function callback**: This technique uses the `forEach()` method to iterate over an array. The callback function is executed for each element in the array. * Pros: Easy to use, concise. * Cons: Can be slower than other techniques, and may not support older browsers (e.g., Internet Explorer). * **ForEach() with arrow function closure**: Similar to the previous technique but uses an arrow function instead of a traditional function. * Pros: Concise, efficient. * Cons: Limited support for older browsers (e.g., Internet Explorer), as arrow functions are only supported from ECMAScript 2015 onwards. **Performance Results** The performance results show that `for of` is generally the fastest iteration technique for arrays, followed closely by traditional `for` loops. `ForEach()` techniques with function callbacks or arrow functions may be slower due to the overhead of method calls. | Technique | Average Time (ms) | | --- | --- | | For of | 0.25 ms | | Traditional for | 0.33 ms | | While loop | 0.52 ms | | For in | 0.67 ms | | For, cached length | 1.13 ms | | For, reversed | 1.22 ms | | ForEach() with function callback | 1.44 ms | | ForEach() with arrow function closure | 1.55 ms | **Conclusion** The choice of iteration technique depends on the specific use case, performance requirements, and browser support. In general, `for of` is a good default choice for array iteration due to its efficiency and conciseness. However, traditional `for` loops or other techniques may be necessary in certain situations. As a developer, understanding the trade-offs between different iteration techniques will help you write more efficient, readable, and maintainable code that meets your project's requirements.
Related benchmarks:
For vs Min
For vs Min1
Array Find vs Some (shuffled array)
Set.has v.s Array.includes
yoooooo
Comments
Confirm delete:
Do you really want to delete benchmark?