Over a million developers have joined DZone.

Loop Through An Array Without Wasteful Lookups

·
Looking up the length of an array on each iteration of a loop is slow and inefficient (if the length of the array isn't changing).

If it's OK to iterate from back to front:
for ( var i=myArray.length-1; i>=0; --i ){
  ...
}

If you need to go from front to back:
for ( var i=0, len=myArray.length; i
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}