Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array loop vs foreach vs for_of
(version: 0)
Comparing performance of:
foreach vs for vs for_of
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 1000; i++) { arr[i] = i; } function someFn(i) { return i * 3 * 8; }
Tests:
foreach
arr.forEach(item => someFn(item))
for
for (let i = 0; i < arr.length; i++) { someFn(arr[i]) }
for_of
for (const item of arr) { someFn(item) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
foreach
for
for_of
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):
**What is being tested?** The provided JSON represents a JavaScript microbenchmark that compares the performance of three different loops to iterate over an array: `forEach`, `for`, and `for...of`. The benchmark creates an array with 1000 elements, initializes each element with its index multiplied by a specific formula (`i * 3 * 8`), and then defines a function `someFn` that performs the same calculation. **Options compared** The three loops being compared are: 1. `forEach`: This loop uses the `Array.prototype.forEach()` method to iterate over the array, which calls the provided callback function once for each element in the array. 2. `for`: This loop uses a traditional `for` loop with an index variable (`i`) to iterate over the array elements. 3. `for...of`: This loop uses the `for...of` loop syntax to iterate over the array elements, which is a newer feature introduced in ECMAScript 2015. **Pros and Cons of each approach** * **forEach**: Pros: + Concise and expressive code + Easy to read and understand * Cons: + Can be slower than traditional loops due to the overhead of the `forEach()` method call + May not be suitable for large arrays or performance-critical applications * **for**: Pros: + Fast and efficient, as it uses a traditional loop with an index variable + Suitable for large arrays or performance-critical applications * Cons: + Less concise and expressive code compared to `forEach` + May require more manual memory management (e.g., using `push()` or `splice()`) * **for...of**: Pros: + Concise and expressive code, similar to `forEach` + Easy to read and understand + Suitable for large arrays or performance-critical applications * Cons: + May not be supported by older browsers or JavaScript engines **Library** None of the provided loops rely on a specific library. **Special JS features or syntax** The `for...of` loop uses the new `for...of` loop syntax, which is a feature introduced in ECMAScript 2015. This syntax allows for more concise and expressive iteration over arrays, objects, and other iterable data structures. **Other alternatives** If you're interested in exploring alternative approaches to iterating over arrays, you might consider using: * `map()`: A method that applies a transformation function to each element of an array and returns a new array with the results. * `reduce()`: A method that reduces an array by applying a reduction function to each element and accumulating a result. * Custom iteration using `Array.prototype.forEach()` or other methods. These alternatives may have different performance characteristics, use cases, and code readability compared to the traditional loops being compared in this benchmark.
Related benchmarks:
Array loop vs for of loop vs foreach vs map (2)
Array loop vs foreach vs map (Small arrays)
Array loop: forEach vs for vs map vs for of entries
Array loop vs foreach vs map with large array
Comments
Confirm delete:
Do you really want to delete benchmark?