Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var object = {}, i = 0;
while (i++ < 100000) {
object['user' + i] = i;
}
// fastest
console.time('keys for');
var result;
var keys = Object.keys(object);
for (var i = 0, l = keys.length; i < l; i++) {
result = object[keys[i]];
}
console.timeEnd('keys for');
console.time('keys forEach');
var result, keys = Object.keys(object);
keys.forEach(function(key){
result = object[key];
});
console.timeEnd('keys forEach');
// slowest
console.time('in hasOwnProperty');
var key, result;
for(key in object) {
if (object.hasOwnProperty(key)) {
result = object[key];
}
}
console.timeEnd('in hasOwnProperty');
console.time('in');
var key, result;
for(key in object) {
result = object[key];
}
console.timeEnd('in');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.