Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for (cache length) vs foreach vs for..in vs for..of
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for in vs for..of
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100);
Tests:
for
var arrLength = array.length; for (var i = 0; i < arrLength; i++) { array[i]; }
foreach
array.forEach(function(i) { array[i]; });
for in
for (var i in array) { array[i]; }
for..of
for (var i of array) { array[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
foreach
for in
for..of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Brave/1 Version/26.4 Safari/605.1.15
Browser/OS:
Safari 26 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
348992.6 Ops/sec
foreach
444478.2 Ops/sec
for in
7160882.5 Ops/sec
for..of
203755.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Definition:** The benchmark is designed to compare the performance of four different loop types in JavaScript: 1. `for` loop 2. `forEach` loop (a part of the Array prototype) 3. `for...in` loop (an older way of looping through arrays, using the `in` keyword) 4. `for...of` loop (a newer way of looping through arrays and other iterable objects, introduced in ECMAScript 2015) **Options Compared:** The benchmark is comparing the performance of these four loop types on a fixed input: an array of length 100. **Pros and Cons:** * **`for` loop:** Simple, efficient, and widely supported. However, it can be less readable for some developers who are not familiar with its syntax. * **`forEach` loop:** More modern and concise than `for` loops, but can be slower due to the overhead of the callback function. It's also less flexible, as it only allows a single callback function to be executed. * **`for...in` loop:** Less efficient than `for` loops and `forEach` loops, especially for large arrays. This is because `for...in` requires the browser to iterate through the array's prototype chain, which can slow down the execution. * **`for...of` loop:** More efficient than `for` loops and `forEach` loops, as it allows for direct access to array elements without the need for indexing or callbacks. It's also more readable and concise. **Library Used:** None explicitly mentioned in the benchmark definition, but `forEach` is a part of the Array prototype, which is a built-in JavaScript library. **Special JS Feature/Syntax:** The `for...of` loop uses a new syntax introduced in ECMAScript 2015. This loop type allows for direct access to iterable objects' elements without needing indexing or callbacks. The browser's implementation of this feature may vary, which can affect performance. **Other Considerations:** * **Browser support:** The benchmark results are from Chrome 115 on a desktop platform. Browser support and performance may vary across different browsers, operating systems, and devices. * **Optimization opportunities:** The benchmark does not consider optimization techniques such as loop unrolling, caching, or SIMD instructions, which can further improve performance. **Alternatives:** * Other loop types, such as `while` loops or `for-each` loops in other programming languages (e.g., Python's `enumerate()` function). * Performance profiling tools and libraries that can help identify bottlenecks and optimize code for specific use cases. * Specialized JavaScript frameworks or libraries designed to improve performance, such as WebAssembly or Just-In-Time (JIT) compilers.
Related benchmarks:
for (cached length) vs foreach vs some
for cached vs foreach vs some vs for..of
for vs foreach for (cached length) vs for..of
for vs for cached length vs foreach vs some vs for..of
Comments
Confirm delete:
Do you really want to delete benchmark?