Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for of / forEach.
(version: 2)
Comparing performance of:
for of vs forEach
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var number = +prompt('Enter array length') var arr = new Array(number).fill().map((_, i) => i); function sum(a, b) { return a + b; }
Tests:
for of
for (const i of arr) { sum(i, i); }
forEach
arr.forEach((i) => { sum(i, i); });
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:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36
Browser/OS:
Chrome 141 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for of
6808805.5 Ops/sec
forEach
18863042.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested. **Benchmark Definition** The benchmark is testing two approaches for iterating over an array: `for of` loop and `forEach()` method. **Options Compared** There are only two options being compared: 1. **For of loop**: This approach uses a `for...of` loop to iterate over the elements of the array. The loop will iterate until it exhausts all elements in the array. 2. **ForEach() method**: This approach uses the `forEach()` method to iterate over the elements of the array. The callback function is called for each element in the array. **Pros and Cons** **For of Loop:** Pros: * Can be faster because it avoids the overhead of a function call (i.e., the callback function passed to `forEach()`) * Can be more efficient for large arrays because it only increments the loop variable (`const i`) once per iteration, whereas the callback function in `forEach()` may reassign the variable. Cons: * Requires support for the `for...of` syntax, which was introduced in ECMAScript 2015 (ES6) * May not be as readable or intuitive for developers unfamiliar with this syntax **ForEach() Method:** Pros: * More readable and easier to understand for developers familiar with functional programming concepts * Supports ES5 syntax, making it compatible with older browsers * Can be used in combination with other array methods (e.g., `map()`) Cons: * May incur a slight performance penalty due to the overhead of the function call * The callback function must reassign the variable (`i`), which may lead to unexpected behavior if not handled carefully **Library and Purpose** There is no library being used in this benchmark. **Special JS Feature or Syntax** The `for...of` loop syntax introduced in ECMAScript 2015 (ES6) is a special feature that allows iterating over arrays without explicitly keeping track of the index. This syntax is supported by most modern browsers and JavaScript engines. **Other Alternatives** If you're interested in exploring other approaches, some alternatives to `for of` loop and `forEach()` method include: * **Array.prototype.indexOf()**: Iterates over the array using a traditional indexing approach. * **Array.prototype.reduce()**: Uses a reduction function to accumulate values from each element in the array. * **Array.prototype.every()**, **Array.prototype.some()**, or other similar methods: These methods can be used with callback functions to iterate over arrays. However, these alternatives are not part of the original benchmark, and their performance may vary depending on the specific use case and requirements.
Related benchmarks:
for..of vs foreach
sum calculation: forEach vs for vs forof vs reduce 2
for of vs Array.reduce vs Array.forEach vs for i for summing and array of integers
for of vs forEach with add operation
Comments
Confirm delete:
Do you really want to delete benchmark?