Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..of vs for..of over entries vs for in vs for cache vs for reverse vs map
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for..of vs for..of over entries vs for..in vs for cache length vs for reverse vs while vs keys vs map
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({length: 100}); var t;
Tests:
for
for (let i = 0; i < array.length; i++) { t = array[i]; }
foreach
array.forEach(function(v, i) { t = v; });
for..of
for (var v of array) { t = v; }
for..of over entries
for (var [i, v] of array.entries()) { t = v; }
for..in
for (var k in array) { t = array[k]; }
for cache length
for (let i = 0,len = array.length; i < len; i++) { t = array[i]; }
for reverse
for (let i = array.length - 1; i >= 0; i--) { t = array[i]; }
while
let i=0,len = array.length while(i < len){ t = array[i] i++ }
keys
for (var i of array.keys()) { t = array[i]; }
map
array.map(function(v, i) { t = v; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (10)
Previous results
Fork
Test case name
Result
for
foreach
for..of
for..of over entries
for..in
for cache length
for reverse
while
keys
map
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 provided JSON and benchmark preparation code to understand what is being tested. **Benchmark Definition** The main goal of this benchmark is to compare the performance of different loop constructs in JavaScript: `for`, `foreach`, `for..of`, `for..of over entries`, `for in`, `map`, and `while`. The comparison aims to determine which approach is the fastest. **Script Preparation Code** The script preparation code creates an array with 100 elements using `Array.from()`. This array will be used as the input for each test case. **HTML Preparation Code** There is no HTML preparation code provided, so it's likely that this benchmark is running in a headless browser environment or on a server-side simulation. Now, let's analyze each individual test case: 1. **`for`**: This is a traditional loop construct that uses an index variable `i` to iterate over the array. 2. **`foreach`**: This method iterates over the array using an iterator, allowing for better support for asynchronous iteration and iterating over collections of values. 3. **`for..of`**: This is a newer loop construct that uses an iterator to iterate over the array, providing more flexibility than traditional `for` loops. 4. **`for..of over entries`**: Similar to `for..of`, but also includes support for iterating over the array's indices and values simultaneously. 5. **`for in`**: This method iterates over the properties of an object (in this case, the array), allowing for easy iteration over the keys or indices of the array. 6. **`map`**: This is a method that creates a new array by applying a transformation function to each element of the original array. While not strictly a loop construct, it's often used in conjunction with loops. 7. **`while`**: This loop construct uses a conditional statement to control the iteration process. **Benchmark Results** The benchmark results show varying performance between different loop constructs, but generally, `for..of` and `foreach` seem to be faster than traditional `for` loops. The `map` method performs reasonably well, while `for in` is slower due to its object-oriented nature. Some observations: * The `while` loop is often the slowest due to its reliance on manual index management. * The `for..of over entries` construct offers a balance between performance and flexibility, making it a viable option for many use cases. * The `foreach` method's performance is consistent across different environments, suggesting that it's well-optimized. * The `map` method's performance can vary depending on the specific transformation function used. Keep in mind that these results are specific to this particular benchmark and may not reflect real-world usage patterns.
Related benchmarks:
map vs forEach Chris
map vs forEach Chris v2b
foreach vs for..of
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
Comments
Confirm delete:
Do you really want to delete benchmark?