ForInVsFor

JavaScript performance comparison

Revision 5 of this test case created

Preparation code

<script>
  var a = [];
  for (var j = 0; j < 100; j++)
  a[j] = Math.random();

  function each(a, f) {
    for (var i = 0, l = a.length; i < l; i++) {
      f(a[i], i);
    }
  }
  var s = 0;
  function add(item) {
    window.s += item;
  }
</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
ForIn
for (var i in a) {
  s += a[i];
}
pending…
ForLoop
for (var i = 0; i < a.length; i++) {
  s += a[i];
}
pending…
ForLoop cached length
for (var i = 0, l = a.length; i < l; i++) {
  s += a[i];
}
pending…
ForEach native
a.forEach(function(item, index) {
  window.s += item;
});
pending…
ForEach native cached func
a.forEach(add);
pending…
Each
each(a, function(item, index) {
  window.s += item;
});
pending…
Each cached func
each(a, add);
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