Which for loop should be used for arrays and JavaScript objects?

I always use for (i=0; i<array.length; i++) when going through arrays.

I always use for (var i in object) when going through the properties of an object.

I cannot use for (i=0; ... ) for the properties of an object, but I can use for (var in ...) for arrays, because arrays are also objects.

The question I ask is this: should I completely remove for (i=0; ... ) and use for (var in ...) for both arrays and objects? Is there any success? Why should I use one over the other?

+4
source share
5 answers

Should I reset for

You should not. for..in when used for loop arrays does not care about the index, and it will also list the properties attached to the object. Stick to for for arrays and for..in for objects.

Excerpt from MDN :

for..in should not be used to iterate over an array where the index order is important ... There is no guarantee that for..in will return indexes in any particular order and return all enumerated properties ...

As far as performance is concerned, I would not worry about this because for..in for circular indexed arrays is obviously not recommended.

+2
source

I made jsperf for you:

http://jsperf.com/for-vs-for-in43

Basically, this is performance testing, and you can see a huge drop in performance when using for(var i in array) .

However, you cannot drop the for for for in .

+2
source

cannot use for (i = 0; ...) the properties of an object, but I can use for (var in ...) for arrays, because arrays are also objects.

You should use for , as the other answers already claim.

But you can use Object.keys(yourObject) to display the keys of the objects as an array, and then use the for loop in that array.

 var keys = Object.keys(myObject); for(var i = 0, key; key = keys[i]; i++) { //... } 
+1
source

If you want to have responsive code, then:

for while( l--) arrays and for(i=0,..) loops for(i=0,..) are the fastest ...

in arrays of objects that you can only use for ... but if your code is good, it doesn’t often happen that you go through the object.

fastest cycles:

 var myarray=[1,2,3,4,5,6], length=myarray.length;//here i cache the length of the array. 

Array length caching is another important thing to save code quickly.

 while(length--){ //do somethingwith your array //myarray[length]; } 

while it was the fastest loop in old browsers .. in new browsers, it looks like a for loop works a little faster.

 for(var i=0;i<length;i++){ //do somethingwith your array //myarray[i]; } 

now, if you want to define variables, do it before the loop.

There is also a nice simplex that shows you performance, even if sometimes the code is not written very well.

http://jsperf.com/fors-vs-while/61

therefore, if you plan to loop through an array, always use while - or for (var i = 0 ..) length caching.

another loop that I really like if you have arrays in multidimensional objects like json is

 for(var a=0,b;b=my.very.long.object[a];++a){ //do somethingwith your array //b } 

another mistake most ppl makes is to use push in a loop ... doing push in a loop means u executes a new function every time. since we already have an “i”, which is an index, we can set it and use it to store data directly without performing a new function

So

 //wrong for(var i=0,newArray=[];i<length;i++){ newArray.push(myarray[i]);//this is a waste of time and resources } //right for(var i=0,newArray=[];i<length;i++){ newArray[i]=myarray[i];//we already have an index. } 

ps.:feel to fix my bad english, but don't touch the code!

+1
source

Note also that the order of enumeration is not guaranteed, so for .. in an array they can return values ​​from a sequence. It will also list all properties, including those that are not numeric (that is, will not be returned by iterating over the index) and enumerated properties in the [[Prototype]] object chain, so you should always use it with a test hasOwnProperty if you want only your own properties (the most common case).

If you are using an ES5 host, you have methods like Object.getOwnPropertyNames and Object.keys to get your own properties. But again, the order is not guaranteed.

Finally, the for, do, and while loops are highly optimized, the performance difference is very small, and the speed in one browser may be slow in another. Just use what is appropriate and optimized only if performance is a problem.

0
source

Source: https://habr.com/ru/post/1486953/


All Articles