Shawn31313
Shawn31313

Reputation: 6052

Why are for..in faster than for loops when looping through an array

I have been wonder where a for loop or a for..in loop would be farther on an array.

I have tested this using jsperf.

My For Loop had 16,676,377 op/s
while the for..in only had 519,853 op/s

So why is:

var a = ["hi", "hi2", "bye", "bye2"];

for (var i in a) {
  return a[i];
}

Slower compared to:

var a = ["hi", "hi2", "bye", "bye2"];

for (var i = 0; i < a.length; i++) {
  return a[i];
}

Upvotes: 0

Views: 1521

Answers (4)

Salvador Dali
Salvador Dali

Reputation: 222541

The answer for your question is simple: for in loop was not created to deal with arrays and does additional things as well and should not be used.

The main purpose of the for-in statement is to iterate thought object's properties, not through the array. This statement will also go into the array prototype chain, iteration through inherited properties and I think you do not need this or even do not know about this.

Another funny thing that you even do not know in what order it will be iterated.

So the main thing to remember - do not use for in with arrays. Only with objects.

P.S as RobG correctly added:

A for loop will search for properties on the [[Prototype]] chain too if they aren't found on the array. E.g. a for loop over [0,,2] will search for 1 all the way to Object.prototype[[Prototype]]

Upvotes: 3

pixelbadger
pixelbadger

Reputation: 1596

It really depends on the JavaScript engine implementation. Because JavaScript is dynamically typed, the interpreter has to do some work to establish the type of a given variable. I suspect there are optimisations in the engine for the classic integer iterator for loop that aren't available when using for...in.

EDIT:

for..in iterates through the enumerable properties of a variable, so var i is assigned the value of each string in your array on each loop.

Upvotes: 0

Amy
Amy

Reputation: 7466

To explain why a for loop is faster than a for in loop is basically understand the underlying data structures used to store the data in memory. Looping through an indexed based array is naturally faster because of the way an array is stored in memory. It's just a location in memory and the items in the array are stored in consecutive locations, aka. in order and next to each other. Generally it's fast to retrieve elements in consecutive order because you don't have to spend time to find it, you just know the next item is always the next location beside it. And because it knows the array.length and can determine which chunks of memory has been portioned off for the array.

Upvotes: 0

rodneyrehm
rodneyrehm

Reputation: 13557

There are a couple of things wrong here.

  1. your return in the loop's body causes the loop to abort after the first iteration, your tests are useless
  2. for..in loops over an object's properties, an array's elements are not its only properties! were you to add a property, such as a.foo = true; that would be included in iterating with for..in but not in for.

Please don't use for..in to loop arrays. Not. Ever.

Upvotes: 1

Related Questions