FOR loop performance in Javascript

Posted by AndrewMcLagan on Stack Overflow See other posts from Stack Overflow or by AndrewMcLagan
Published on 2012-10-09T09:27:45Z Indexed on 2012/10/09 9:37 UTC
Read the original article Hit count: 207

As my research leads me to believe that for loops are the fastest iteration construct in javascript language. I was thinking that also declaring a conditional length value for the for loop would be faster... to make it clearer, which of the following do you think would be faster?

Example ONE

for(var i = 0; i < myLargeArray.length; i++ ) {
    console.log(myLargeArray[i]);
} 

Example TWO

var count = myLargeArray.length;
for(var i = 0; i < count; i++ ) {
    console.log(myLargeArray[i]);
} 

my logic follows that on each iteration in example one accessing the length of myLargeArray on each iteration is more computationally expensive then accessing a simple integer value as in example two?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about Performance