Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs for-of
(version: 0)
Comparing performance of:
forEach vs for-of
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
forEach
const array = [{ id: 1, name: 'Bob' }, { id: 2, name: 'Sarah' }, { id: 2, name: 'Amy' }, { id: 2, name: 'Joe' }]; try { array.forEach(function(item, idx) { if (idx === 2) { throw new Error(); } }); } catch (ignore) { }
for-of
const array = [{ id: 1, name: 'Bob' }, { id: 2, name: 'Sarah' }, { id: 2, name: 'Amy' }, { id: 2, name: 'Joe' }]; let idx = 0; for (const item of array) { if (idx === 2) { break; } idx++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
forEach
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's being tested. **Benchmark Overview** The benchmark compares two approaches to iterate over an array in JavaScript: 1. `forEach` loop 2. `for-of` loop (also known as "iterator-based for loop") **Options Compared** The `forEach` approach uses a traditional loop with the `Array.prototype.forEach()` method, which iterates over the array using an iterator. The `for-of` approach uses a special type of loop that allows iterating directly over an array's elements without the need for indexing or iterator objects. This loop is optimized for modern JavaScript engines. **Pros and Cons** **forEach Approach:** Pros: * More readable code, as it explicitly iterates over each element in the array. * Easier to understand for developers familiar with traditional loops. Cons: * Can be slower due to the overhead of creating an iterator object and iterating over it. **for-of Approach:** Pros: * Faster execution speed, as modern JavaScript engines can optimize this type of loop more effectively. * More concise code, making it easier to write and read. Cons: * Less readable for developers unfamiliar with this specific syntax. * May require additional setup or context to understand the iteration logic. **Library Used (if any)** There is no library used in this benchmark. Both `forEach` and `for-of` are built-in JavaScript constructs. **Special JS Feature/Syntax** The `for-of` loop uses a special syntax, introduced in ECMAScript 2015 (ES6), which allows iterating directly over an array's elements without the need for indexing or iterator objects. Here's a brief explanation of this syntax: ```javascript for (const item of array) { // code here } ``` This loop is designed to work with arrays, but it can also be used with other iterable values like sets and maps. The `const` keyword is used to declare the variable `item`, which takes on each element value in the iteration. **Other Alternatives** If you need to iterate over an array, there are additional alternatives: 1. **Traditional `for` loop**: Similar to the `forEach` approach, but uses a traditional index-based loop. ```javascript for (let i = 0; i < array.length; i++) { // code here } ``` 2. **Array.prototype.map()`, Array.prototype.filter()**, or Array.prototype.reduce()**: While not exactly iteration over an array, these methods can be used to process arrays in a functional programming style. In this benchmark, the `for-of` approach is shown to be faster than the traditional `forEach` loop, highlighting its potential for performance advantages in modern JavaScript engines.
Related benchmarks:
Iteration through array; of vs forEach
foreach vs for..of
.forEach vs for const of
foreach vs for...of
forEach vs for of 7
Comments
Confirm delete:
Do you really want to delete benchmark?