Created
March 4, 2013 04:37
-
-
Save ericz/5079988 to your computer and use it in GitHub Desktop.
Proper for each loops for JS dicts.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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