Fastest array loops in Javascript

JavaScript performance comparison

Revision 469 of this test case created by

Preparation code

<script>
  // Populate the base array
  var baseCount = 10000;
  var baseArr = new /*Array*/Uint16Array(baseCount);
  for (var i = 0; i < baseCount; i++) {
    baseArr[i] = i;
  }
  var len = baseArr.length;
  function someFn(ix) {
    return ix * 5 + 1 / 3 * 8;
  }
</script>
      
<script>
Benchmark.prototype.setup = function() {
  arr = baseArr /* baseArr.slice() */;

};
</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
Test1
var i = 0;

for (; i < len; i += 1) {
  someFn(arr[i]);
}
pending…
Test2
var i;

for (i = 0; i < len; i += 1) {
  someFn(arr[i]);
}
pending…
For loop, cached
pending…
without length calculation
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