I've been unable to find the answer to this, say for example you have the array:
var myArray = ["a","b","c","d","e","f","g"]; // pretend I define up to 1000 elements
One simple approach to iterate through the array would be:
for(var i=0; i<myArray.length; i++){
console.log(myArray[i]);
}
What I'm trying to figure out is if the .length property will be evaluated on each iteration, then if I use:
my len = myArray.length;
for(var i=0; i<len i++){
console.log(myArray[i]);
}
Does the 2nd approach will be a performance improvement ? Does the JS engine will calculate the length on each iteration?
It does! I like this solution
for(var i=0, len=myArray.length; i<len; i++){
console.log(myArray[i]);
}
It's just a little cleaner than the one you pasted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With