forEach vs for in

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var arr = [];
    for (var i = 0; i < 10000; i++) {
      arr[i] = "value-"+ i;
    }
   
    var obj = {};
    for (var i = 0; i < 10000; i++) {
      obj[i] = "value-"+ i;
    }
};
</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
forEach
arr.forEach(function(item) {
  item;
});
pending…
for in
for (key in obj) {
  obj[key];
}
pending…
for i
for (var i = 0; i < arr.length; i++) {
  arr[i];
}
pending…

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

Compare results of other browsers

0 comments

Add a comment