Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array loop vs foreach vs for xxx
(version: 0)
Comparing performance of:
foreach vs for vs for of
Created:
5 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
let x; arr.forEach(function (item){ x = someFn(item); }) const y = x;
for
let x = 0; for (var i = 0, len = arr.length; i < len; i++) { x = someFn(arr[i]); } const y = x;
for of
let x = 0; for (const item of arr) { x = someFn(item); } const y = x;
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):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Overview** The benchmark compares the performance of three different approaches to iterate over an array in JavaScript: 1. **Array Loop**: Using a traditional `for` loop with indexing. 2. **Foreach**: Using the `forEach` method. 3. **For-of**: Using the `for...of` loop. **Options Compared** The benchmark is comparing these three options because they are all ways to iterate over an array in JavaScript. The choice of iteration method can significantly impact performance, especially for large arrays or complex iterations. **Pros and Cons of Each Approach** 1. **Array Loop**: This approach is simple and straightforward but can be slow due to the overhead of indexing and checking bounds. * Pros: Easy to understand and implement. * Cons: Can be slower than other approaches. 2. **Foreach**: This approach uses a method call and can be faster than array looping, but it requires additional function calls and may incur overhead. * Pros: Can be faster than array looping, easier to read. * Cons: Requires additional function calls, may incur overhead. 3. **For-of**: This approach is the most modern and efficient way to iterate over arrays in JavaScript, as it avoids indexing and bounds checks altogether. * Pros: Fastest option, easy to understand and implement. * Cons: May not be supported by older browsers. **Library Used** None of the provided benchmark tests use any external libraries. The script preparation code only includes a simple `someFn` function that is used in all three test cases. **Special JavaScript Feature or Syntax** The `for-of` loop is a relatively new feature introduced in ECMAScript 2015 (ES6). It provides a more concise and efficient way to iterate over arrays, tuples, and other iterable objects. The `forEach` method also uses this optimized iteration mechanism under the hood. **Other Alternatives** There are other ways to iterate over arrays in JavaScript, such as: * Using `map`, `filter`, or `reduce` methods for more complex iterations. * Using `Array.prototype.reduceRight()` or `Array.prototype.forEachRight()` * Using a library like Lodash or Ramda for functional programming. However, these alternatives are not tested in the provided benchmark.
Related benchmarks:
Array loop vs foreach
Array loop vs for of loop vs foreach vs map (2)
Array loop: forEach vs for vs map vs for of entries
Array loop vs foreach vs map with large array
Array loop vs foreach vs for_of
Comments
Confirm delete:
Do you really want to delete benchmark?