performance - How to efficiently count the number of keys/properties of an object in JavaScript? -
what's fastest way count number of keys/properties of object? it possible without iterating on object? i.e. without doing
var count = 0; (k in myobj) if (myobj.hasownproperty(k)) count++;
(firefox did provide magic __count__
property, removed somewhere around version 4.)
to in es5-compatible environment, such node, chrome, ie 9+, ff 4+, or safari 5+:
object.keys(obj).length
- browser compatibility
- object.keys documentation
- (includes method can add non-ecma5 browsers)
Comments
Post a Comment