object literal iteration

JavaScript performance comparison

Revision 5 of this test case created by Pencroff

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var obj = {};
  for (var i = 0; i < 10000; i++) {
    obj['key' + i] = 'value' + i;
  }
  
  var res = [];

};

Benchmark.prototype.teardown = function() {
  res = [];

};
</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
forEach in Obj.keys()
// In ECMA5 Object.key(obj) returns only iterable props in the object 
// that pass obj.hasOwnProperty

Object.keys(obj).forEach(function(key) {
  res.push(obj[key]);
});
pending…
prop in obj
for (var prop in obj) {
  if (obj.hasOwnProperty(prop)) {
    res.push(obj[prop]);
  }
}
pending…
prop in Obj.keys()
// In ECMA5 Object.key(obj) returns only iterable props in the object 
// that pass obj.hasOwnProperty

for (var prop in Object.keys(obj)) {
  res.push(obj[prop]);
}
pending…
for loop Obj.keys()
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
  res.push(obj[keys[i]]);
}
pending…
Obj.key forEach bind
// In ECMA5 Object.key(obj) returns only iterable props in the object 
// that pass obj.hasOwnProperty

Object.keys(obj).forEach(function(key) {
  res.push(obj[key]);
}.bind(this));
pending…
prop in obj no hasOwnProperty
for (var prop in obj) {
  res.push(obj[prop]);
}
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