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:
4 years ago
by:
Guest
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]; });
for in
for (var i in array) { array[i]; }
for..of
for (var i of array) { array[i]; }
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:
7 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:150.0) Gecko/20100101 Firefox/150.0
Browser/OS:
Firefox 150 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
34002732.0 Ops/sec
foreach
11599118.0 Ops/sec
for in
132674416.0 Ops/sec
for..of
2053741.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is being tested in the provided JSON benchmark. **Benchmark Purpose:** The goal of this benchmark is to compare the performance of different loop iteration methods: `for`, `foreach`, `for...in`, and `for...of`. **Loop Iteration Methods Compared:** 1. **`for` Loop**: This is a traditional loop that uses an index variable (`i`) to iterate over the elements of an array. 2. **`forEach` Method**: This is a built-in method on arrays that allows you to execute a callback function for each element in the array. 3. **`for...in` Loop**: This loop iterates over the properties of an object (in this case, an array), including its indices. 4. **`for...of` Loop**: This is a newer loop iteration method introduced in ECMAScript 2015 (ES6) that allows you to iterate over the values of an array without using an index variable. **Pros and Cons:** * `for` Loop: + Pros: Simple, easy to understand, and widely supported. + Cons: Can be less efficient than other methods due to the need to manually manage the index variable. * `forEach` Method: + Pros: Concise and easy to read. No need to worry about index management. + Cons: May not be as fast as other methods due to the overhead of a method call. * `for...in` Loop: + Pros: Can iterate over both indices and values, but note that it iterates over properties, not elements. + Cons: Not ideal for iterating over arrays, as it may include non-enumerable properties. Also, its performance can be slower due to the additional overhead of accessing properties. * `for...of` Loop: + Pros: Newer and more modern method that allows for concise iteration without index management. It's also optimized for performance. + Cons: May require newer JavaScript engines to execute. **Library/Technology Used:** None mentioned in the benchmark JSON, but it is likely that MeasureThat.net uses a browser emulator or a JavaScript engine like V8 (used by Chrome) to run the benchmarks. **Special JS Features/Syntax:** The `for...of` loop uses a new syntax introduced in ES6. If you're not familiar with it, don't worry! It's a relatively modern feature that allows for concise iteration over arrays without using an index variable. **Alternatives:** If you're interested in exploring other alternatives for loop iteration, here are some: * `while` Loop: A traditional loop that uses a conditional statement to control the iteration. * `do...while` Loop: Similar to a `while` loop but executes the loop body at least once before checking the condition. * Array methods like `map()`, `filter()`, and `reduce()` can also be used for iteration, although they may have different use cases and performance characteristics. I hope this explanation helps you understand what's being tested in the benchmark!
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?