getOwnPropertyNames vs keys (v3)

Revision 3 of this benchmark created by Arthur on


Description

Get the size of an hash.

Setup

"use strict";
    
    var keys = Object.keys,
      getOwnPropertyNames = Object.getOwnPropertyNames;
    
    var useKeys = function(o) {
      var used = keys(o),
        i = 0,
        len = used.length;
    
      for (; i < len; i++) {
        noop(used[i]);
      }
    }, useGOPN = function(o) {
        var used = getOwnPropertyNames(o),
          i = 0,
          len = used.length;
    
        for (; i < len; i++) {
          noop(used[i]);
        }
      };
    
    var noop = function() {};
    
    function oldWay(obj) {
      for (var prop in obj) {
        if (obj.hasOwnProperty(prop)) {
          noop(prop);
        }
      }
    }
    
    function O() {
      this.a = true;
      this.b = true;
      this.c = [1, 2, 3];
      this.spoke = {
        a: true
      }
      this.f = function() {};
    }
    O.prototype.abc = function() {};
    var o = new O();

Test runner

Ready to run.

Testing in
TestOps/sec
Object.getOwnPropertyNames
useGOPN(o);
ready
Object.keys
useKeys(o);
ready
for .. in
oldWay(o);
ready

Revisions

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