Look at what's happening differently in each iteration:

for( var i = 0; i < p1.length; i++ ) 
  1. Check if i < p1.length
  2. Increment i by one

Very simple and fast.

Now look at what's happening in each iteration for this:

for( var i in p1 )

Repeat

  1. Let P be the name of the next property of obj whose [[Enumerable]] attribute is true. If there is no such property, return (normal, V, empty).

It has to find next property in the object that is enumerable. With your array you know that this can be achieved by a simple integer increment, where as the algorithm to find next enumerable is most likely not that simple because it has to work on arbitrary object and its prototype chain keys.

Answer from Esailija on Stack Overflow
🌐
Leanylabs
leanylabs.com › blog › js-foreach-map-reduce-vs-for-for_of
Performance of JavaScript .forEach, .map and .reduce vs for and for..of
With .map, the benchmarks were identical, and with for..of the results aren’t that different and might be just a benchmark fluke. ... Both for and for..of are 3.5 times faster than reduce. However, the loops are much more verbose:
Discussions

Javascript for..in vs for loop performance - Stack Overflow
That's a huge time difference just because of the way I wrote the for loop. I normally don't use for..in loop for arrays but for some reason I used it while writing this function. Can someone explain why there is this huge difference in performance between these 2 styles? More on stackoverflow.com
🌐 stackoverflow.com
For-Of Loop vs. For Loop - javascript
If we used this example, besides performance benefits (if any), why would someone use one loop over the other? If there are any other examples/instances that'd be great as well. ... In the first case i will be whatever items the array holds. In the second, i would be the indexes of the array. So, no - they aren't interchangeable. And why would they be? ... In this case, yes. But in the general case for... More on stackoverflow.com
🌐 stackoverflow.com
Why is for...of loop so much slower than the traditional for loop?
I setup a simple benchmark for comparing performance of for (const x of arr) and for (let i = 0; i < arr.length; i++) loops. More on stackoverflow.com
🌐 stackoverflow.com
java - Is there a performance difference between a for loop and a for-each loop? - Stack Overflow
Worth mentioning that in a for-each loop there is no way to access an index counter (since it doesn't exist) 2009-02-06T11:42:56.227Z+00:00 ... There is the performance penalty of allocating the iterator. I had some highly parallel code in an Android live wallpaper. More on stackoverflow.com
🌐 stackoverflow.com
🌐
C# Corner
c-sharpcorner.com › article › c-sharp-performance-of-code-for-loop-vs-for-each-loop
C# Performance Of Code - For Loop VS Foreach Loop
September 25, 2023 - The for loop version uses enough stack space for only two local variables (counter and i). The Foreach version, on the other hand, uses stack space for four locals (item, AccountList object, and two compiler-generated temporaries). When a method is called in the CLR, all of the memory required for the locals is allocated upon the stack.
Top answer
1 of 4
29

Look at what's happening differently in each iteration:

for( var i = 0; i < p1.length; i++ ) 
  1. Check if i < p1.length
  2. Increment i by one

Very simple and fast.

Now look at what's happening in each iteration for this:

for( var i in p1 )

Repeat

  1. Let P be the name of the next property of obj whose [[Enumerable]] attribute is true. If there is no such property, return (normal, V, empty).

It has to find next property in the object that is enumerable. With your array you know that this can be achieved by a simple integer increment, where as the algorithm to find next enumerable is most likely not that simple because it has to work on arbitrary object and its prototype chain keys.

2 of 4
9

As a side note, if you cache the length of p1:

var plen = p1.length;
for( var i = 0; i < plen; i++ )

you will get a slight speed increase.

...And if you memoize the function, it will cache results, so if the user tries the same numbers you will see a massive speed increase.

var eDistance = memoize(euclideanDistance);  

function memoize( fn ) {  
    return function () {  
        var args = Array.prototype.slice.call(arguments),  
            hash = "",  
            i = args.length;  
        currentArg = null;  
        while (i--) {  
            currentArg = args[i];  
            hash += (currentArg === Object(currentArg)) ?  
            JSON.stringify(currentArg) : currentArg;  
            fn.memoize || (fn.memoize = {});  
        }  
        return (hash in fn.memoize) ? fn.memoize[hash] :  
        fn.memoize[hash] = fn.apply(this, args);  
    };  
}

eDistance([1,2,3],[1,2,3]);
eDistance([1,2,3],[1,2,3]); //Returns cached value

credit: http://addyosmani.com/blog/faster-javascript-memoization/

🌐
DEV Community
dev.to › siddiqus › which-for-loop-is-the-fastest-in-javascript-4hdf
Which for-loop is the fastest in JavaScript? - DEV Community
December 25, 2022 - For this test, I tried four different for-loop syntaxes, where each loop was simply incrementing a counter variable 10^8 times, and each loop ran five times to get an average time. Here are the results: ... Interestingly, for very small arrays (< 10^3 elements) it does not really make a difference. However, for 10^3 to 10^6 size, the result is a bit surprising. ... So when it comes to <10^6 arrays, the classic forEach seems to be just fine. I personally used to use the for-of (slowest) syntax because it seemed like the easiest one, well now we know which one I'm going to start using!
Top answer
1 of 2
21

A bit of backnowledge to explain your question:

In Javascript, Objects (among these are Arrays) store properties in key-value pairs. That means that each assigned value has a key (the property name) to access it. For example in

person[name] = 'Tom'

person is the Object, name the key and 'Tom' the corresponding value.

Arrays use indices (i.e. numbers) as keys:

array[5] = 10

Now, keep that in mind in the following explanation.

Let's start with the traditional for loop:

for(let i = 0; i < array.length; i++) {...}

This way of iterating over an array is the oldest of the bunch (as long as you are not using while-loops) You'll find a corresponding way of writing a for loop in pretty much any (imperative) programming language. You'll notice, it is very explicit in the way it works. E.g. you could change the break-condition i < array.length to something else (e.g. i < array.length-1 for skipping the last position), or the step i++ (e.g. to i+=2), or start at a different index (e.g. let i = 5). You can iterate backwards instead of forwards through the array, if you want. Pretty much anything you can do in another for loop, you can do in this kind as well, if you know how.

Inside the brackets {...} you can then use i as a key to access the arrays values

Now this is all very powerful and nice, but it gets cumbersome to write every time, especially if in most of the cases you just want to iterate over an array. But luckily we have for-in:

For-in will retrieve you all the keys you have set yourself. With an array, you can use that to achieve the same result as above using

for(let i in array) {...}

Note however, that for-in is not only gonna give you the number keys from an array. It is also gonna work on other keys you have set yourself on any object:

let object = {
    key1 : 'value',
    key2 : 'value'
}

for(let i in object) {
    console.log(i)
}

will log out 'key1' and 'key2' (yes, the keys as well are strings here).

For a bit more precise description of what keys exactly it will give you, have a look at the link below.

When would you use for-in? Whenever you want to call some code for each element of an array / (almost) each property of an object once. E.g. when you want to increment all values in an array by 1. When not to use for-in? Don't use it if you need more granular control over the order you traverse the array in, or you don't want to hit all elements of the array, but only every second/third.

For an excellent resource on for-in loops I recommend Mozilla Developer Network

So, what are for-of loops then?

For-of loops are syntactically (i.e. the way you write them) very similar to for-in loops:

for(let v of array) {...}

However, for one, they are only going to work on so-called iterable objects (arrays are iterable). Second, you only get the values. They are no longer going to give you any keys!

let array = ['a', 'b', 'c']
for(let v of array) {
    console.log(v)
}

logs 'a', 'b' and 'c'. You won't know anymore, what keys these values had!

So, when to use these? Every time you just need the values and don't care about the keys. E.g. if you just want to print the values. When not to use them? If you want to swap elements of an array, if you want to reverse order. You can't even increment the values by 1 and store them back into the array, because for that, you would need to know their corresponding key.

For more information on for-in loops as well as what iterable actually means, again, I recommend the MDN

2 of 2
2

The only key differences that you'll have to consider would be

  • Say you want to start your loop from a specific index, you can do that with your traditional for loop but not using for...of.

  • You can't access indices of your array elements natively. You'll need to go for a workaround to achieve this.

      //Cannot access indices
      for(const num of [11,22,33,44,55]){
         console.log(num);
      }
    
    
      //Workaround
      for(const [index, num] of [11,22,33,44,55].entries()){
         console.log(num, index);
      }
    
🌐
DEV Community
dev.to › maafaishal › benchmarking-for-while-forof-and-arrayforeach-using-performancenow-1jjg
Benchmarking 'for', 'while', 'for...of', and 'Array.forEach' - using Performance.now() - DEV Community
July 20, 2023 - It's reasonable to expect a performance hit for forEach, given that it's a method call that takes a callback, but I would've expected for...of to do better given it's a built-in language feature. I'm still gonna use for...of for most loops though, unless it's a critical path that frequently operates on very large arrays.
Find elsewhere
🌐
Medium
medium.com › @trig79 › javascript-the-early-lessons-for-vs-foreach-vs-for-of-loops-iteration-which-one-is-better-b557f385045
JavaScript The Early Lessons: For Vs forEach Vs For Of. Loops & Iteration, Which One Is Better? | by Trig | Medium
April 12, 2022 - In the case where you may handle 10000+ rows of data, what happens when we find the result in the first few 100 rows? Well, nothing we have to sit and wait for the loop to iterate through every single row before it finishes. It is at this point I begin to understand why this does become a discussion about performance and I begin to understand why people are so vocal on the topic.
Top answer
1 of 16
224

From Item 46 in Effective Java by Joshua Bloch :

The for-each loop, introduced in release 1.5, gets rid of the clutter and the opportunity for error by hiding the iterator or index variable completely. The resulting idiom applies equally to collections and arrays:

// The preferred idiom for iterating over collections and arrays
for (Element e : elements) {
    doSomething(e);
}

When you see the colon (:), read it as “in.” Thus, the loop above reads as “for each element e in elements.” Note that there is no performance penalty for using the for-each loop, even for arrays. In fact, it may offer a slight performance advantage over an ordinary for loop in some circumstances, as it computes the limit of the array index only once. While you can do this by hand (Item 45), programmers don’t always do so.

2 of 16
32

All these loops do the exact same, I just want to show these before throwing in my two cents.

First, the classic way of looping through List:

for (int i=0; i < strings.size(); i++) { /* do something using strings.get(i) */ }

Second, the preferred way since it's less error prone (how many times have YOU done the "oops, mixed the variables i and j in these loops within loops" thing?).

for (String s : strings) { /* do something using s */ }

Third, the micro-optimized for loop:

int size = strings.size();
for (int i = -1; ++i < size;) { /* do something using strings.get(i) */ }

Now the actual two cents: At least when I was testing these, the third one was the fastest when counting milliseconds on how long it took for each type of loop with a simple operation in it repeated a few million times - this was using Java 5 with jre1.6u10 on Windows in case anyone is interested.

While it at least seems to be so that the third one is the fastest, you really should ask yourself if you want to take the risk of implementing this peephole optimization everywhere in your looping code since from what I've seen, actual looping isn't usually the most time consuming part of any real program (or maybe I'm just working on the wrong field, who knows). And also like I mentioned in the pretext for the Java for-each loop (some refer to it as Iterator loop and others as for-in loop) you are less likely to hit that one particular stupid bug when using it. And before debating how this even can even be faster than the other ones, remember that javac doesn't optimize bytecode at all (well, nearly at all anyway), it just compiles it.

If you're into micro-optimization though and/or your software uses lots of recursive loops and such then you may be interested in the third loop type. Just remember to benchmark your software well both before and after changing the for loops you have to this odd, micro-optimized one.

Top answer
1 of 2
222

I recommend to always use for … of in ES6.

  • It works on any iterable.
  • It supports all kinds of control flow in the loop body, like continue, break, return, yield and await.

Also I personally find it more readable, but that comes down to preference. Some people think forEach is a more functional style, but that's wrong – it has no result value and is all about doing side effects, so an imperative-looking loop fits that purpose better.

Performance is not a concern: in modern engines all loop styles are equivalent.

2 of 2
83

This is a very interesting question which has been discussed in many other sites. I'll post the basics of what I have read.

ForEach exclusively belong to the royal family of Arrays. The forEach method was introduced with lineage to the prototypal inheritance of Array object! Needless to say, the forEach clause works only with those data structure which are Arrays. The method basically iterates over the elements of the array and executes a callback function [basically some executable function/ fun activity].


The for-of loop is adequately new to the JS world and packs in super-powers! Voilaaaaaaa! The for-of loop creates a loop iterating over iterable member objects. The list is an extensive one such as

  • Array
  • Map
  • Set
  • String
  • TypedArray
  • Other W3C classes

You need to know that this bad-ass boy emerged with the birth of ES6 in 2015. So, it offers plenty of flexibility in usage


Performance

In performance, for...of is faster than forEach. Results can be found here

forEach is 24% slower than for...of


Update

There are several other iterable classes in the W3C specification, like FileList, as I mentioned above. And in recent drafts of W3C (around when ES6 was released), collections like HTMLCollection and NodeList now implement forEach() as well, not just Array anymore. By @Patrick Roberts


Source Links:

  • https://codeburst.io/foreach-vs-for-of-vs-for-in-tug-of-for-d8f935396648
  • https://www.reddit.com/r/javascript/comments/4spd5b/forof_vs_foreach/
🌐
Jsben
jsben.ch › article › javascript-loop-performance-for-vs-foreach-vs-map
JavaScript Loop Performance: `for` vs. `forEach` vs. `map` - JSBEN.CH
January 13, 2026 - However, there are fundamental differences in how loops are processed. In this article, we look at the raw numbers to decide when you should prioritize performance over syntax sugar. Let's pit the most common iteration methods against each other: The Classic (for): Maximum control, direct index access. The Modern (for...of): Cleaner syntax, works with iterators.
🌐
Better Programming
betterprogramming.pub › which-is-the-fastest-while-for-foreach-for-of-9022902be15e
Which Is the Fastest: While, For, forEach(), For…of? | by Jonathan Hsu | Better Programming
December 1, 2019 - According to those who commented on my previous article, the for...of technique is superior in speed and readability to .forEach(). Since I don’t write larger-scale applications where processing time really comes into play, I was curious to find something out. Just how fast (or slow) is each type of iteration structure? So I decided to test things out and share my results. Before we get started, let’s do a 30-second recap on the different types of iteration structures. While Loop: The simplest of the looping mechanisms.
Top answer
1 of 16
31

That clearly depends on the particular implementation of the interpreter/compiler of the specific language.

That said, theoretically, any sane implementation is likely to be able to implement one in terms of the other if it was faster so the difference should be negligible at most.

Of course, I assumed while and for behave as they do in C and similar languages. You could create a language with completely different semantics for while and for

2 of 16
16

In C#, the For loop is slightly faster.

For loop average about 2.95 to 3.02 ms.

The While loop averaged about 3.05 to 3.37 ms.

Quick little console app to prove:

 class Program
    {
        static void Main(string[] args)
        {
            int max = 1000000000;
            Stopwatch stopWatch = new Stopwatch();

            if (args.Length == 1 && args[0].ToString() == "While")
            {
                Console.WriteLine("While Loop: ");
                stopWatch.Start();
                WhileLoop(max);
                stopWatch.Stop();
                DisplayElapsedTime(stopWatch.Elapsed);
            }
            else
            {
                Console.WriteLine("For Loop: ");
                stopWatch.Start();
                ForLoop(max);
                stopWatch.Stop();
                DisplayElapsedTime(stopWatch.Elapsed);
            }
        }

        private static void WhileLoop(int max)
        {
            int i = 0;
            while (i <= max)
            {
                //Console.WriteLine(i);
                i++;
            };
        }

        private static void ForLoop(int max)
        {
            for (int i = 0; i <= max; i++)
            {
                //Console.WriteLine(i);
            }
        }

        private static void DisplayElapsedTime(TimeSpan ts)
        {
            // Format and display the TimeSpan value.
            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                ts.Hours, ts.Minutes, ts.Seconds,
                ts.Milliseconds / 10);
            Console.WriteLine(elapsedTime, "RunTime");
        }
    }
🌐
Bigscal
bigscal.com › frontend
Choosing the Best JavaScript Loop for Optimal Performance
February 5, 2026 - If you need to repeat a block of code to fix javascript loop control, javascript loop count javascript loop x times, you can use it for loops. Traditionally, the for loop is the fastest, so you should always use them, right? Not necessarily.
🌐
Incredible-web
incredible-web.com › blog › performance-of-for-loops-with-javascript
Performance of for loops with JavaScript
July 7, 2016 - After having performed these tests 10 times each, we get 4 time lap averages, one per technology, that can be summed up in this chart: We can clearly see on this graph that "foreach" loops are slower than normal loops and that Lodash is more ...
Top answer
1 of 16
463

Patrick Smacchia blogged about this last month, with the following conclusions:

  • for loops on List<T> are a bit more than 2 times cheaper than foreach loops on List<T>.
  • Looping on array is around 2 times cheaper than looping on List<T>.
  • As a consequence, looping on array using for is 5 times cheaper than looping on List<T> using foreach (which I believe, is what we all do).
2 of 16
195

First, a counter-claim to Dmitry's (now deleted) answer. For arrays, the C# compiler emits largely the same code for foreach as it would for an equivalent for loop. That explains why for this benchmark, the results are basically the same:

using System;
using System.Diagnostics;
using System.Linq;

class Test
{
    const int Size = 1000000;
    const int Iterations = 10000;

    static void Main()
    {
        double[] data = new double[Size];
        Random rng = new Random();
        for (int i=0; i < data.Length; i++)
        {
            data[i] = rng.NextDouble();
        }

        double correctSum = data.Sum();

        Stopwatch sw = Stopwatch.StartNew();
        for (int i=0; i < Iterations; i++)
        {
            double sum = 0;
            for (int j=0; j < data.Length; j++)
            {
                sum += data[j];
            }
            if (Math.Abs(sum-correctSum) > 0.1)
            {
                Console.WriteLine("Summation failed");
                return;
            }
        }
        sw.Stop();
        Console.WriteLine("For loop: {0}", sw.ElapsedMilliseconds);

        sw = Stopwatch.StartNew();
        for (int i=0; i < Iterations; i++)
        {
            double sum = 0;
            foreach (double d in data)
            {
                sum += d;
            }
            if (Math.Abs(sum-correctSum) > 0.1)
            {
                Console.WriteLine("Summation failed");
                return;
            }
        }
        sw.Stop();
        Console.WriteLine("Foreach loop: {0}", sw.ElapsedMilliseconds);
    }
}

Results:

For loop: 16638
Foreach loop: 16529

Next, validation that Greg's point about the collection type being important - change the array to a List<double> in the above, and you get radically different results. Not only is it significantly slower in general, but foreach becomes significantly slower than accessing by index. Having said that, I would still almost always prefer foreach to a for loop where it makes the code simpler - because readability is almost always important, whereas micro-optimisation rarely is.

🌐
Medium
dev-aditya.medium.com › for-of-vs-foreach-which-one-is-better-25de7e8e9ff3
for...of vs forEach Which one is better? | by Aditya Yadav | Medium
October 3, 2024 - Results may vary based on the JavaScript engine and environment, but typically for...of is faster and more memory-efficient. ... You need better performance and memory efficiency, especially with large datasets.