Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
For vs Foreach vs While
(version: 1)
Comparing performance of:
For vs Foreach vs While
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = new Array(100);
Tests:
For
for (var i = 0; i < array.length; i++) { array[i]; }
Foreach
array.forEach(function(i) { array[i]; });
While
var i = 0; while (i < 100) { array[i]; i++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
For
Foreach
While
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
For
100323.0 Ops/sec
Foreach
2434194.2 Ops/sec
While
197508.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark test cases and explain what's being tested. **Benchmark Definition** The JSON defines a simple JavaScript microbenchmark that compares three different loops: 1. `for` loop 2. `forEach` loop 3. `while` loop All three loops aim to iterate over an array of 100 elements, performing some operation on each element (in this case, simply accessing the element using its index). **Script Preparation Code** The script preparation code creates a new array with 100 elements: ```javascript var array = new Array(100); ``` This code is executed before running each test case. **Html Preparation Code** There is no HTML preparation code provided, which means that the benchmark only tests the JavaScript engine's performance and does not consider other factors like page rendering or DOM manipulation. **Test Cases** The three individual test cases are: 1. `For` loop: ```javascript for (var i = 0; i < array.length; i++) { array[i]; } ``` This loop uses a traditional `for` loop with an index variable (`i`) and increments it manually. 2. `Foreach` loop: ```javascript array.forEach(function(i) { array[i]; }); ``` This loop uses the `forEach` method, which is a built-in array method that iterates over the elements of an array. 3. `While` loop: ```javascript var i = 0; while (i < 100) { array[i]; i++; } ``` This loop uses a traditional `while` loop with an index variable (`i`) and increments it manually. **Library Used** None of the test cases use any external libraries or frameworks. The `forEach` method is a built-in JavaScript method that comes with the language. **Special JS Feature/ Syntax** None of the test cases use any special JavaScript features or syntax beyond what's described above. **Pros and Cons of Different Approaches** Here are some pros and cons of each approach: 1. **For Loop** * Pros: Easy to understand and implement, minimal overhead. * Cons: Can be slower for large arrays due to the need for manual index management. 2. **Foreach Loop** * Pros: Convenient and efficient, eliminates the need for manual index management. * Cons: May have additional overhead due to the use of a built-in method. 3. **While Loop** * Pros: Similar to the `for` loop in terms of control flow, but with more flexibility. * Cons: Requires manual index management, which can be error-prone. **Other Alternatives** In addition to these three loops, other alternatives for iterating over arrays in JavaScript include: 1. `Array.prototype.map()` 2. `Array.prototype.reduce()` 3. Using `for...of` loop (available in modern browsers and Node.js) However, these alternatives are not included in the provided benchmark test cases. **Benchmark Result Interpretation** The latest benchmark result shows the execution speed for each test case on a Mac OS X 10.15.7 system with Chrome 122 browser: 1. `Foreach` loop: 2434194.25 executions per second 2. `While` loop: 197508.46875 executions per second 3. `For` loop: 100322.9765625 executions per second The results suggest that the `Foreach` loop is the fastest, followed by the `For` loop, and then the `While` loop. However, the actual performance difference may vary depending on the specific use case and environment.
Related benchmarks:
foreach vs for..of
For vs Foreach vs Do While vs While
foreach vs for...of
For vs Foreach vs Do While vs While v3
Comments
Confirm delete:
Do you really want to delete benchmark?