object iteration

JavaScript performance comparison

Revision 8 of this test case created by dotCY

Info

Which case is faster for iterating of an object with much keys.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj = {};
   
    for (var i = 0; i < 50; i++) {
      obj["a" + i] = "a";
    }
   
    var empty = [];
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
for-with-hasOwnProperty
for (var x in obj) {
  if (obj.hasOwnProperty(x)) {
    empty.push(obj[x]);
  }
};
pending…
object-keys-forEach
Object.keys(obj).forEach(function(v) {
  empty.push(obj[v]);

});
pending…
object-keys-for
var arr = Object.keys(obj);
var j = arr.length;
for (var x = 0; x < j; x++) {
  empty.push(obj[arr[x]]);
}
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. Here’s a list of current revisions for this page:

0 comments

Add a comment