Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for of vs foreach vs for 123
(version: 0)
Comparing performance of:
for of vs forEach vs for
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = new Array(10000).map((v, i) => i)
Tests:
for of
let result = 0; for (const item of arr) { result += item; }
forEach
let result = 0; arr.forEach(i => result += i)
for
let result = 0; for (let i = 0; i < arr.length; i++) { result += arr[i]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for of
forEach
for
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's being tested. **Benchmark Overview** The benchmark compares three different approaches to iterating over an array in JavaScript: `for of`, `forEach`, and traditional `for` loops. The goal is to determine which approach is the fastest. **Script Preparation Code** The script preparation code creates a new array with 10,000 elements, where each element is its index (`i`) using the arrow function syntax: ```javascript var arr = new Array(10000).map((v, i) => i); ``` This ensures that the array has a consistent structure and size across all test cases. **Html Preparation Code** There is no HTML preparation code provided, which means the benchmark only runs in Node.js or other JavaScript environments without a DOM. **Test Cases** The three test cases are: 1. **`for of`**: Uses the `for...of` loop to iterate over the array. 2. **`forEach`**: Uses the `forEach()` method to iterate over the array. 3. **`for`**: Uses a traditional `for` loop with an index variable (`i`) to iterate over the array. **Library Usage** There is no library usage mentioned in the benchmark definition or test cases. However, some browsers (like Chrome) may use optimized implementations of these constructs that are not explicitly exposed as libraries. **Special JavaScript Features/Syntax** * The `for...of` loop is a relatively recent feature introduced in ECMAScript 2015 (ES6). It's designed to iterate over iterable objects, such as arrays, and provides a more concise syntax. * The `forEach()` method is also a built-in method for iterating over arrays. **Pros and Cons of Each Approach** Here are some general pros and cons of each approach: 1. **`for...of`**: * Pros: More concise syntax, easier to read and write, works with any iterable object. * Cons: May have slower performance due to the overhead of creating an iterator. 2. **`forEach()`**: * Pros: Concise syntax, easy to use, works with arrays and other iterables. * Cons: May not be as readable or maintainable as `for...of` or traditional `for` loops. 3. **Traditional `for` Loop**: * Pros: Well-established syntax, easy to understand, allows for fine-grained control over iteration. * Cons: More verbose, may have slower performance due to the need to manually increment and check indices. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: 1. **`map()`**: Instead of using `for...of`, `forEach()`, or traditional `for` loops, you can use the `map()` method to create a new array with transformed values. 2. **Generators**: Generators are a type of iterable that allow you to yield values one at a time, which can be useful for creating iterative algorithms. 3. **Reduce()`: The `reduce()` method is another built-in method for iterating over arrays and accumulating values. These alternatives may offer different trade-offs in terms of performance, readability, or maintainability, so it's worth exploring each option to see which best fits your use case.
Related benchmarks:
Iteration through array; of vs forEach
Array loop vs foreach vs map populate array
Array.forEach vs Object.keys().forEach
Array fill map, vs for i loop
forEach vs for of 7
Comments
Confirm delete:
Do you really want to delete benchmark?