getOwnPropertyNames vs keys (v2)

Revision 2 of this benchmark created by DotNetWise on


Description

Get the size of an hash.

Setup

"use strict";
    
    var keys = Object.keys,
        getOwnPropertyNames = Object.getOwnPropertyNames;
    
    function oldWay(obj) {
      var a = [];
      for (var prop in obj) {
        if (obj.hasOwnProperty(prop)) {
          a.push(prop);
        }
      }
      return a;
    }
    
    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
Object.getOwnPropertyNames(o).length
ready
Object.keys
Object.keys(o).length
ready
for .. in
oldWay(o).length
ready

Revisions

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