Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for / forEach
(version: 0)
Comparing performance of:
for..of vs Array.forEach
Created:
2 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var array = new Array(100);
Tests:
for..of
for (var i = 0; i < array.length; i++) { console.log(array[i]); }
Array.forEach
array.forEach(function(i) { console.log(array[i]); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for..of
Array.forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 YaBrowser/24.1.0.0 Safari/537.36
Browser/OS:
Yandex Browser 24 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Array.forEach
4.7M/s
for..of
3K/s
View exact numbers
Test name
Executions per second
✓
Array.forEach
4,666,836 Ops/sec
for..of
2,723 Ops/sec
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 Definition** The benchmark is testing two different approaches to iterate over an array: 1. **For loop with traditional `array.length`**: The first approach uses a traditional for loop, where the variable `i` iterates from 0 to `array.length - 1`, logging each element of the array. 2. **Array method `forEach`**: The second approach uses the `forEach` method provided by the Array prototype, which executes a callback function once for each element in the array, logging each element. **Options Compared** The two options being compared are: * Traditional for loop with manual iteration (`array.length`) * Array method `forEach` **Pros and Cons of Each Approach** 1. **Traditional For Loop**: * Pros: + More control over the iteration process + Can be used with other data structures like objects or arrays with non-numeric indices * Cons: + Requires manual incrementation of the loop variable (`i`) + May have performance overhead due to unnecessary checks 2. **Array Method `forEach`**: * Pros: + Concise and readable syntax + Built-in optimization for performance + Can be used with other data structures like arrays or objects * Cons: + Less control over the iteration process + May have a slight performance overhead due to function call overhead **Library Used (if applicable)** There is no library explicitly mentioned in the benchmark definition. However, the `forEach` method is a part of the Array prototype, which is a built-in JavaScript feature. **Special JS Feature or Syntax** The benchmark uses a traditional for loop with manual iteration (`array.length`). There are no special features or syntaxes used beyond this simple iteration pattern. **Other Considerations** When choosing between these two approaches, consider the following factors: * Readability: If readability is crucial, `forEach` might be a better choice due to its concise syntax. * Control: If you need fine-grained control over the iteration process, traditional for loop might be a better fit. * Performance: Both methods have similar performance characteristics in most cases. However, if you're iterating over large arrays or performing complex operations, `forEach` with its built-in optimization might provide a slight edge. **Alternative Approaches** Other approaches to iterate over an array could include: * For...of loop (similar to traditional for loop but with more modern syntax) * Using `reduce()` method * Using `map()` method (though it returns a new array, not modifying the original) These alternatives might be worth considering in different scenarios or when working with specific data structures.
Related benchmarks
For vs Foreach vs While
Comments
Confirm delete:
Do you really want to delete benchmark?