Skip to content

Instantly share code, notes, and snippets.

@ericz
Created March 4, 2013 04:37
Show Gist options
  • Save ericz/5079988 to your computer and use it in GitHub Desktop.
Save ericz/5079988 to your computer and use it in GitHub Desktop.
Proper for each loops for JS dicts.
// BAD
for (var key in this._workingPeers) {
var peer = this._workingPeers[key];
}
// GOOD
var indices = Object.keys(this._workingPeers);
for (var i = 0; i < indices.length; i++) {
var peer = this._workingPeers[indices[i]];
}
/*
Reasons:
- For in looks up prototype chain and is slow
- For in requires you to use .hasOwnProperty or it will report properties up the prototype chain
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment