I discovered recently on the NodeJS list that iterating over an object's
properties using Object.keys() is faster than for..in loops. I didn't
believe it at first, but tested it out and discovered that the 2nd loop
below runs about 20-25% faster:
for (var ii in obj) {
fn(ii, obj[ii]);
}
var keys = Object.keys(obj);
for (var ii = 0; ii < keys.length; ++ii) {
var key = keys[ii];
fn(key, obj[key]);
}
This holds true if `obj` has 0 keys or if it has 100k keys (although when
you get up to 100k keys it's only negligibly faster).
As far as I can tell the only effective difference between the two loops is
that for..in will iterate over all enumerable properties, while
Object.keys() will only give you enumerable own keys. Is the walk down the
prototype chain in the for..in case happening several times? I'd like to
understand particularly what causes this performance difference! This is v8
3.7.3 by the way.
--
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users