Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
iterate over array
(version: 0)
Comparing performance of:
for-of vs foreach
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [...new Array(10)] var map = new Map();
Tests:
for-of
for (const [index] of array.entries()) { map.set(index, 'test'); }
foreach
array.forEach((_, index) => { map.set(index, 'test') })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for-of
foreach
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
gemma2:9b
, generated one year ago):
This benchmark tests the performance of two different methods for iterating over an array in JavaScript: `for...of` and `forEach`. **Options Compared:** * **`for...of`:** This modern loop iterates directly over the values of an array, making it concise and readable. * **`forEach`:** This method takes a callback function as an argument which is executed for each element in the array. **Pros and Cons:** * **`for...of`**: * **Pros:** More concise syntax, often considered more readable. * **Cons:** Can be slightly less performant than `forEach` in some cases due to extra steps involved in getting both index and value with each iteration. * **`forEach`**: * **Pros:** Potentially slightly faster performance as it directly executes the callback function without needing to explicitly access the index. * **Cons:** Can be considered less readable than `for...of` for simple iterations, especially when you need both value and index. **Other Considerations:** The benchmark doesn't explicitly show the impact of array size. Performance differences between `for...of` and `forEach` can become more significant with larger arrays. **Alternatives:** There are other ways to iterate over an array in JavaScript: * **`Array.map()`:** Creates a new array by applying a function to each element of the original array. * **`Array.filter()`:** Creates a new array containing elements that pass a test specified by a provided function. **Libraries and Special Features:** This benchmark doesn't use any specific libraries or utilize advanced JavaScript features beyond standard built-in methods for arrays and iterators.
Related benchmarks:
foreach and map
Array range generating
Array.from vs Array.prototype.map
Map.forEach vs Array.forEach vs Array.from(Map.prototype.values()).forEach
Comments
Confirm delete:
Do you really want to delete benchmark?