object-looping

JavaScript performance comparison

Revision 2 of this test case created by Jonathan Perry

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var obj = {
    a: 1,
    b: 2,
    c: 3,
    d: 4,
    f: 5
  },
  i = 0,
  _hasOwnProperty = Object.prototype.hasOwnProperty,
  value = null,
  keys = Object.keys(obj)
  
  function each (obj, fn) {
    var key;
    for (key in obj) {
      if (_hasOwnProperty.call(obj, key)){
        fn.call(obj, obj[key])
      }
    }
  }
  
  function each2 (obj, fn) {
      var keys = Object.keys(obj)
      for (var i=0, key; key = keys[i++]; fn.call(obj, key, obj[key]));
  }
  
  function withEach(key) {
     value = obj[key];
  }

};
</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, if
for (key in obj) {
  if (_hasOwnProperty.call(obj, key)) {
     value = obj[key];
  }
}
pending…
for in, continue
for (key in obj) {
  if (!_hasOwnProperty.call(obj, key)) { 
    continue;
  } else {
    value = obj[key];
  }
}
pending…
Object.keys()
Object.keys(obj).forEach(withEach);
pending…
Object.keys() cached
keys.forEach(withEach);
pending…
Keys for;
for (var keys = Object.keys(obj), i = 0, ln = keys.length; i < ln; i++) {
    value = obj[key];
}
pending…
each, for in
each(obj, function (value) { });
pending…
for in, if (=== true)
for (key in obj) {
  if (_hasOwnProperty.call(obj, key) === true) {
     value = obj[key];
  }
}
pending…
for in, continue (!== true)
for (key in obj) {
  if (_hasOwnProperty.call(obj, key) !== true) { 
    continue;
  } else {
    value = obj[key];
  }
}
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