Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
foreach vs for...of
(version: 0)
Compare loop performance
Comparing performance of:
foreach vs for..of
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100);
Tests:
foreach
array.forEach(function(i) { array[i]; });
for..of
for (var i of array) { array[i]; }
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
foreach
7898286.0 Ops/sec
for..of
971801.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The "foreach vs for...of" benchmark compares the performance of two loop constructs in JavaScript: `forEach` and `for...of`. The test case uses an array with 100 elements to compare the execution times of both loops. **Loop Constructs Compared** Two loop constructs are compared: 1. `forEach`: This is a built-in method on arrays that iterates over each element, calling the provided callback function for each iteration. 2. `for...of`: This is a new loop construct introduced in ECMAScript 2015 (ES6) that allows iterating over iterable objects, such as arrays. **Options Compared** The benchmark compares two options: 1. **Using `forEach`**: The first option uses the `forEach` method to iterate over the array. 2. **Using `for...of`**: The second option uses the `for...of` loop construct to iterate over the array. **Pros and Cons of Each Approach** Here are some pros and cons of each approach: * **Using `forEach`**: + Pros: Easy to use, no need to declare a variable or increment an index. + Cons: May incur additional overhead due to the creation of a callback function object on each iteration. * **Using `for...of`**: + Pros: More expressive and intuitive for iterating over arrays, eliminates the need for a callback function. + Cons: Requires declaring a variable (in this case, `i`) and incrementing its value manually. **Library Used** There is no explicit library used in the benchmark. However, it's worth noting that some browsers may use internal libraries or optimizations to execute JavaScript loops, which can affect the results of the benchmark. **Special JS Features/Syntax** The test case uses ES6 features: * `for...of` loop construct * Template literals (`\r\n`) in the script preparation code **Other Considerations** When comparing loop performance, it's essential to consider other factors that might impact execution time, such as: * Cache locality: How efficiently do the loops access and reuse data within the array? * Branch prediction: Do the loops contain conditional statements that can affect branch prediction? **Alternative Benchmarks** Some alternative benchmarks that compare loop constructs include: * **V8 Benchmark Suite**: A set of benchmarks developed by Google's V8 JavaScript engine to evaluate its performance. * **JSPerf**: An online benchmarking tool for comparing the performance of different JavaScript engines and libraries. These alternatives can provide more comprehensive insights into the performance characteristics of various loop constructs, as well as other aspects of JavaScript execution.
Related benchmarks:
for vs foreach vs some
for (cached length) vs foreach vs some
for vs foreach vs some big
foreach vs for..of
Comments
Confirm delete:
Do you really want to delete benchmark?