Empty Object Test (v8)

Revision 8 of this benchmark created by Eric Blade on


Description

Compare Object.keys to for(key in obj) for testing if an object is empty

Setup

var obj = {},
      obj_sm = {},
      i;
    
    for (i = 0; i < 10000; i++) {
      obj["abc_" + i] = null;
    }
    
    for (i = 0; i < 10; i++) {
      obj_sm["abc_" + i] = null;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Object.keys
Object.keys(obj).length;
ready
for var in obj
for (var key in obj) {
  if (obj.hasOwnProperty(key)) break
}
ready
Object.keys small
Object.keys(obj_sm).length;
ready
for var in obj small
for (var key in obj_sm) {
  if (obj_sm.hasOwnProperty(key)) break
}
ready
JSON.stringify
JSON.stringify(obj) === '{}'
ready
JSON.stringify Small
JSON.stringify(obj_sm) === '{}'
ready
getOwnPropertyNames
Object.getOwnPropertyNames(obj).length === 0
ready
getOwnPropertyNames small
Object.getOwnPropertyNames(obj_sm).length
ready
Object.keys 2
Boolean(obj && typeof obj == 'object') && !Object.keys(obj).length
ready
Object.keys small 2
Boolean(obj_sm && typeof obj_sm == 'object') && !Object.keys(obj_sm).length
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.