, ! 10 exmpty, Object.keys(), :)
Node, Chrom, Firefox IE 9, , :
- (... in...) - !
- Object.keys (obj) .length is 10 times slower for empty objects
- JSON.stringify (obj) .length is always the slowest (not surprising)
- Object.getOwnPropertyNames (obj) .length takes longer than Object.keys (obj) .length can be much longer on some systems.
Bottom line effect, use:
function isEmpty(obj) {
for (var x in obj) { return false; }
return true;
}
or
function isEmpty(obj) {
for (var x in obj) { if (obj.hasOwnProperty(x)) return false; }
return true;
}
See detailed test results and test code for Is the object empty?
source
share