Multiple .hasOwnProperty() vs Object.keys() lookups

Benchmark created by Bemi Faison on


Description

All that a obj.hasOwnProperty() method can return is in Object.keys(obj). Is it faster lookup an index than inspect an object?

Setup

var
      obj = {
        aaa: 1,
        bbb: 1,
        ccc: 1,
        ddd: 1,
        eee: 1,
        fff: 1,
        ggg: 1,
        hhh: 1,
        iii: 1,
        jjj: 1
      },
      cachedObjKeys = Object.keys(obj),
      objKeys
    ;

Test runner

Ready to run.

Testing in
TestOps/sec
hasOwnProperty
obj.hasOwnProperty('aaa');
obj.hasOwnProperty('bbb');
obj.hasOwnProperty('ccc');
obj.hasOwnProperty('ddd');
obj.hasOwnProperty('eee');
obj.hasOwnProperty('fff');
obj.hasOwnProperty('ggg');
obj.hasOwnProperty('hhh');
obj.hasOwnProperty('iii');
obj.hasOwnProperty('jjj');
 
ready
Object.keys + indexOf
objKeys = Object.keys(obj);
~objKeys.indexOf('aaa');
~objKeys.indexOf('bbb');
~objKeys.indexOf('ccc');
~objKeys.indexOf('ddd');
~objKeys.indexOf('eee');
~objKeys.indexOf('fff');
~objKeys.indexOf('ggg');
~objKeys.indexOf('hhh');
~objKeys.indexOf('iii');
~objKeys.indexOf('jjj');
 
ready
cached keys + indexOf
~cachedObjKeys.indexOf('aaa');
~cachedObjKeys.indexOf('bbb');
~cachedObjKeys.indexOf('ccc');
~cachedObjKeys.indexOf('ddd');
~cachedObjKeys.indexOf('eee');
~cachedObjKeys.indexOf('fff');
~cachedObjKeys.indexOf('ggg');
~cachedObjKeys.indexOf('hhh');
~cachedObjKeys.indexOf('iii');
~cachedObjKeys.indexOf('jjj');
 
ready

Revisions

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