Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for..of (fixed)
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for..of
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100);
Tests:
for
for (var i = 0, len = array.length; i < len; i++) { array[i]; }
foreach
array.forEach(function(item) { item; });
for..of
for (var item of array) { item; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for
foreach
for..of
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. The provided benchmark definition and test cases compare the performance of three different loop constructs: `for`, `foreach`, and `for..of`. We'll break down each option, their pros and cons, and other considerations. **Loop Constructs** 1. **For Loop** ```javascript for (var i = 0, len = array.length; i < len; i++) { array[i]; } ``` Pros: * Wide support across browsers and environments. * Easy to understand and implement. Cons: * Can be slower due to the extra iterations required for variable initialization and loop termination checks. 2. **Foreach Loop** ```javascript array.forEach(function(item) { item; }); ``` Pros: * Faster execution compared to traditional `for` loops, as it leverages the engine's optimized array iteration logic. * Reduced boilerplate code. Cons: * May not be supported in older browsers or environments. * Less control over loop iterations and termination conditions. 3. **For..of Loop** ```javascript for (var item of array) { item; } ``` Pros: * Similar to `foreach` but with improved readability and maintainability, thanks to the explicit iteration variable declaration (`var item`). * Wide support across modern browsers and environments. Cons: * May require a minimum JavaScript engine version or feature support. * Less optimal performance compared to traditional `for` loops or `foreach` loops in some cases. Now, let's discuss special considerations for the provided test cases: **Library:** None of the test cases use any external libraries. They only utilize built-in JavaScript features and arrays. **Special JS Features/Syntax:** The test cases do not use any special JavaScript features or syntax that would require additional explanation. However, it's worth noting that the `for..of` loop requires a minimum JavaScript engine version or feature support (ECMAScript 2015+). **Benchmark Results:** The latest benchmark results show that: * The `for..of` loop has the highest execution rate (`ExecutionsPerSecond`) at 5555831.0, followed by the traditional `for` loop at 3435480.75, and finally the `foreach` loop at 117324.4296875. Keep in mind that these results may vary depending on the specific browser, device platform, operating system, and JavaScript engine version used. **Alternatives:** If you're interested in exploring other loop constructs or benchmarking different JavaScript features, MeasureThat.net offers a wide range of benchmarks and test cases. Some examples include: * Iterating over objects with `for...in` or `for...of` * Using regular expressions for string matching * Implementing a simple sorting algorithm (e.g., bubble sort or quicksort) * Comparing the performance of different array methods (e.g., `map`, `filter`, or `reduce`) Feel free to explore these and other benchmarks on MeasureThat.net to gain insights into various JavaScript topics and optimize your code for better performance.
Related benchmarks:
Array fill foreach, vs for i loop
foreach vs for..of
foreach vs for...of
For loop vs <Array>.forEach() vs for...of loop
for (cache length) vs foreach vs for..in vs for..of
Comments
Confirm delete:
Do you really want to delete benchmark?