Skip to content

Instantly share code, notes, and snippets.

@emkay
Created November 17, 2011 23:02
Show Gist options
  • Select an option

  • Save emkay/1374853 to your computer and use it in GitHub Desktop.

Select an option

Save emkay/1374853 to your computer and use it in GitHub Desktop.
For Loops
var a = ['cat', 'dog', 'pig', 'bird'];
// works, but you are accessing the a.length property every time.
for (var i = 0; i < a.length; i += 1) {
console.log(a[i]);
}
// better because it is caching length, especially for DOM collections
for (var i = 0, len = a.length; i < len; i += 1) {
console.log(a[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment