Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs foreach vs for...in vs for...of
(version: 0)
Compare loop performance
Comparing performance of:
for vs foreach vs for in vs for..of
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = new Array(100);
Tests:
for
var len = array.length; for (var i = 0; i < array.length; i++) { array[i]; }
foreach
array.forEach(function(el) { el; });
for in
for (var i in array) { array[i]; }
for..of
for (var el of array) { el; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for
foreach
for in
for..of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36 EdgA/126.0.0.0
Browser/OS:
Chrome Mobile 126 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
40682.7 Ops/sec
foreach
2314313.0 Ops/sec
for in
2016073.6 Ops/sec
for..of
5182500.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested in MeasureThat.net. **Benchmark Definition** The benchmark definition json represents a comparison between different loop constructs in JavaScript: `for`, `foreach`, `for...in`, and `for...of`. The goal is to measure their performance, which will help users determine the most efficient way to iterate over arrays or objects. **Options Compared** Here are the different options being compared: 1. **`for`**: A traditional loop construct that uses a manual increment of the index variable. 2. **`foreach`**: An array method that allows iterating over an array using a callback function. 3. **`for...in`**: A loop construct that iterates over the properties (i.e., keys) of an object, but is not designed for arrays or numerical ranges. 4. **`for...of`**: A newer loop construct introduced in ECMAScript 2015 (ES6), which allows iterating over arrays or other iterable objects. **Pros and Cons** Here's a brief summary of the pros and cons of each option: 1. **`for`**: * Pros: Simple, well-established, and widely supported. * Cons: Can be error-prone due to manual index management. 2. **`foreach`**: * Pros: Concise, easy to read, and eliminates the need for manual indexing. * Cons: May incur overhead due to method invocation. 3. **`for...in`**: * Pros: Flexible and suitable for iterating over objects or arrays with non-numeric keys. * Cons: Not designed for numerical ranges, and may be slower than other options. 4. **`for...of`**: * Pros: Modern, efficient, and well-suited for array iteration. * Cons: May require newer browsers to support. **Libraries Used** The benchmark doesn't explicitly mention any libraries being used in the test cases. However, it's worth noting that the `foreach` loop uses an array method (`array.forEach`), which is a part of the ECMAScript standard. **Special JS Features or Syntax** None of the test cases use special JavaScript features or syntax beyond what's already mentioned (i.e., no ES6+ features like classes, async/await, or template literals). If you were to add more complexity to the benchmark, you might consider using these newer features to further challenge the loop constructs. **Other Alternatives** If you're looking for alternative loop constructs in JavaScript, you could also explore: 1. **`while` loops**: A basic, manual iteration construct. 2. **`do-while` loops**: Similar to `while`, but with a slightly different syntax. 3. **`Array.prototype.reduce()` or `Array.prototype.forEach()`: While not traditional loops, these methods can be used for iterative processing. Keep in mind that the choice of loop construct ultimately depends on your specific use case and performance requirements. MeasureThat.net's benchmark can provide valuable insights to help you make informed decisions about which approach is best suited for your needs.
Related benchmarks:
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
for vs foreach vs for..of vs for..of over entries vs for in
Comments
Confirm delete:
Do you really want to delete benchmark?