Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For vs foreach
(version: 0)
Comparing performance of:
for loop vs for each
Created:
9 years ago
by:
Guest
Jump to the latest result
Tests:
for loop
var arr = []; for (var i = 0; i<10000; i++) { arr.push(i); } var sum = 0; for (var j = 0; j < arr.length; j++) { sum = sum + j; }
for each
var arr = []; for (var i = 0; i<10000; i++) { arr.push(i); } var sum = 0; arr.forEach(function(num) { sum = sum + num; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for loop
for each
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for loop
26604.4 Ops/sec
for each
10775.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the benchmark in detail. **Benchmark Overview** The benchmark compares two approaches for iterating over an array of numbers: traditional `for` loops and `forEach` method. The goal is to measure which approach is faster. **Options Compared** There are two options compared: 1. **Traditional `for` loop**: This approach uses a manual counter variable (`i`) to iterate over the array, with explicit bounds checking. 2. **`forEach` method**: This approach uses the `Array.prototype.forEach()` method, which iterates over the array using a callback function. **Pros and Cons of Each Approach** 1. **Traditional `for` loop**: * Pros: More control over iteration, can be optimized for specific use cases. * Cons: Requires manual bounds checking, can lead to index-related errors if not implemented carefully. 2. **`forEach` method**: * Pros: Convenient and concise syntax, reduces the risk of index-related errors. * Cons: May introduce overhead due to function call overhead, and may not be optimized for specific use cases. **Library and Purpose** In this benchmark, there is no explicit library mentioned. However, the `Array.prototype.forEach()` method is a part of the ECMAScript standard (ES6+) and is widely supported by modern JavaScript engines. **Special JS Feature or Syntax** There are no special JavaScript features or syntax used in this benchmark. The code uses standard ES5 features and syntax. **Other Alternatives** If you're looking for alternatives to `forEach`, some options include: 1. **`map()`**: Can be used to transform the array while iterating, but may not be as suitable for simple iteration use cases. 2. **`reduce()`**: Can be used to accumulate values from an array, but may not be as efficient as `forEach` for pure iteration use cases. **Benchmark Preparation Code** The benchmark preparation code is empty, which means that the script will start execution from scratch with each test case. **Individual Test Cases** There are two test cases: 1. **"for loop"`**: This test case measures the performance of a traditional `for` loop approach. 2. **"for each"`**: This test case measures the performance of the `forEach` method approach. The benchmark results show that the Chromium 52 browser performs better on the "for loop" test case, with an average execution rate of approximately 11,066.45 executions per second. The same browser performs worse on the "for each" test case, with an average execution rate of approximately 4,391.07 executions per second. Keep in mind that these results may vary depending on the specific use case and environment in which the benchmark is run.
Related benchmarks:
Array fill foreach, vs for i loop
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?