Object.keys(t).length-vs-for-in

JavaScript performance comparison

Test case created by valf

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var a6 = {
    a1: 1,
    a2: 2,
    a3: 3,
    a4: 4,
    a5: 5,
    a6: 6
  };
  
  var a12 = {
    a1: 1,
    a2: 2,
    a3: 3,
    a4: 4,
    a5: 5,
    a6: 6,
    c1: 11,
    c2: 22,
    c3: 33,
    c4: 44,
    c5: 55,
    c6: 66,
  }
  
  
  var a24 = {
    a1: 1,
    a2: 2,
    a3: 3,
    a4: 4,
    a5: 5,
    a6: 6,
    c1: 11,
    c2: 22,
    c3: 33,
    c4: 44,
    c5: 55,
    c6: 66,
    b1: 1,
    b2: 2,
    b3: 3,
    b4: 4,
    b5: 5,
    b6: 6,
    d1: 11,
    d2: 22,
    d3: 33,
    d4: 44,
    d5: 55,
    c6: 66
  }
  
  var b24 = {
    a1: 1,
    a2: 2,
    a3: 3,
    a4: 4,
    a5: 5,
    a6: 6,
    c1: 11,
    c2: 22,
    c3: 33,
    c4: 44,
    c5: 55,
    c6: 66,
    b1: 1,
    b2: 2,
    b3: 3,
    b4: 4,
    b5: 5,
    b6: 6,
    d1: 11,
    d2: 22,
    d3: 33,
    d4: 44,
    d5: 55,
    c6: 66
  }
  
  function objectKeys(b) {
    return Object.keys(b).length;
  }
  
  function forIn(b) {
    var i = 0;
    for(var k in b) {
      //if(a.hasOwnProperty(k)) {
        i++;
      //}
    }
    return i;
  }

};

Benchmark.prototype.teardown = function() {
  a6 = undefined;
  a12 = undefined;
  a24 = undefined;
  b24 = undefined;

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
objectKeys (24 attrs)
objectKeys(a24);
pending…
forIn (24 attrs)
forIn(b24);
pending…

Revisions

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

0 Comments