Object.keys vs custom code

Benchmark created on


Preparation HTML

<script>
  var obj = {
    a: 'b',
    c: 1,
    d: ['e', 'f', 'g'],
    h: {
      i: 'j',
      k: 2,
      m: [1, 2, 3]
    }
  }
  
  getObjectKeys = function (object) {
      var key, keys = [];
  
      for (key in object) {
        if (object.hasOwnProperty(key)) keys.push(key);
      }
  
      return keys;
    };
  
  getObjectKeys2 = function (object) {
      if (Object.keys && typeof Object.keys === 'function') return Object.keys(object);
  
      var key, keys = [];
  
      for (key in object) {
        if (object.hasOwnProperty(key)) keys.push(key);
      }
  
      return keys;
    };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
native
var jj = Object.keys(obj);
ready
custom
var jj = getObjectKeys(obj);
ready
native w/fallback
var jj = getObjectKeys2(obj);
ready

Revisions

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