Object.keys-vs-for-in-hasOwnProperty Anonymous Object

JavaScript performance comparison

Revision 6 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var foo = {
    a: "A",
    b: "B",
    c: "C",
    d: "D",
    e: "E",
    f: "F",
    g: "G",
    h: "H",
    i: "I",
    j: "J",
    k: "K",
    l: "L",
    m: "M",
    n: "N",
    o: "O",
    p: "P",
    q: "Q",
    r: "R",
    s: "S",
    t: "T",
    u: "U",
    v: "V",
    w: "W",
    x: "X",
    y: "Y",
    z: "Z"
  };
  
  var bar = Object.create(foo, {
    a: {
      value: "aa"
    },
    b: {
      value: "bb"
    },
    c: {
      value: "cc"
    },
    D: {
      value: "dd"
    },
    E: {
      value: "ee"
    },
    F: {
      value: "ff"
    },
    G: {
      value: "gg",
      enumerable: true
    },
    H: {
      value: "hh",
      enumerable: true
    },
    I: {
      value: "ii",
      enumerable: true
    }
  });
  var hasOwn = foo.hasOwnProperty;
  var example = [];
  var quux = {
    G: "gg",
    H: "hh",
    I: "ii"
  };

};
</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
for-in-loop/hasOwnProperty
var key, val;
example.length = 0;
for (key in bar) {
  if (hasOwn.call(bar, key)) {
    val = bar[key];
    example.push(val);
  }
}
// The results actually differ between Chrome and Firefox?!
// example = ["gg", "hh", "ii", "aa", "bb", "cc"]
// Note: referencing example is required in order to prevent some VMs from optimizing dead code.
if(example.length < 3 || example[0] !== 'gg' || example[1] !== 'hh' || example[2] !== 'ii') {
  console.error('Unexpected results:', example);
}
pending…
Object.keys/for-loop
example.length = 0;
var i, len, key, val, keys = Object.keys(bar);
for (i = 0, len = keys.length; i < len; i++) {
  key = keys[i];
  val = bar[key];
  example.push(val);
}
// example = ["gg", "hh", "ii"]
// Note: referencing example is required in order to prevent some VMs from optimizing dead code.
if(example.length !== 3 || example[0] !== 'gg' || example[1] !== 'hh' || example[2] !== 'ii') {
  console.error('Unexpected results:', example);
}
pending…
Object.keys/forEach
example.length = 0;
Object.keys(bar).forEach(function(key) {
  var val = bar[key];
  example.push(val);
});
// example = ["gg", "hh", "ii"]
// Note: referencing example is required in order to prevent some VMs from optimizing dead code.
if(example.length !== 3 || example[0] !== 'gg' || example[1] !== 'hh' || example[2] !== 'ii') {
  console.error('Unexpected results:', example);
}
pending…
for-in loop
example.length = 0;
var key, val;
for (key in bar) {
  val = bar[key];
  example.push(val);
}
// Note: referencing example is required in order to prevent some VMs from optimizing dead code.
if(example[0] !== 'gg' || example[1] !== 'hh' || example[2] !== 'ii') {
  console.error('Unexpected results:', example);
}
pending…
Object.keys/for-loop/quux
example.length = 0;
var i, len, key, val, keys = Object.keys(quux);
for (i = 0, len = keys.length; i < len; i++) {
  key = keys[i];
  val = quux[key];
  example.push(val);
}
// example = ["gg", "hh", "ii"]
// Note: referencing example is required in order to prevent some VMs from optimizing dead code.
if(example.length !== 3 || example[0] !== 'gg' || example[1] !== 'hh' || example[2] !== 'ii') {
  console.error('Unexpected results:', example);
}
pending…
for-in loop/quux
example.length = 0;
var key, val;
for (key in quux) {
  val = quux[key];
  example.push(val);
}
// example = ["gg", "hh", "ii"]
// Note: referencing example is required in order to prevent some VMs from optimizing dead code.
if(example.length !== 3 || example[0] !== 'gg' || example[1] !== 'hh' || example[2] !== 'ii') {
  console.error('Unexpected results:', example);
}
pending…
for-in-loop/regular hasOwnProperty
var key, val;
example.length = 0;
for (key in bar) {
  if (bar.hasOwnProperty(key)) {
    val = bar[key];
    example.push(val);
  }
}
// example = ["gg", "hh", "ii", "aa", "bb", "cc"]
// Note: referencing example is required in order to prevent some VMs from optimizing dead code.
if(example.length < 3 || example[0] !== 'gg' || example[1] !== 'hh' || example[2] !== 'ii') {
  console.error('Unexpected results:', example);
}
pending…
for-in-loop/hasOwnProperty foo
example.length = 0;
var key, val;
for (key in foo) {
  if(foo.hasOwnProperty(key)) {
    val = foo[key];
    example.push(val);
  }
}

if(example.length < 3) {
  console.error('unexpected results:', example);
}
pending…
for-in-loop/hasOwnProperty quux
example.length = 0;
var key, val;
for (key in quux) {
  if(quux.hasOwnProperty(key)) {
    val = quux[key];
    example.push(val);
  }
}

if(example.length < 3) {
  console.error('unexpected results:', example);
}
pending…
for-in-loop foo
example.length = 0;
var key, val;
for (key in foo) {
  val = foo[key];
  example.push(val);
}

if(example.length < 3) {
  console.error('unexpected results:', example);
}
pending…

Compare results of other browsers

Revisions

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

0 Comments