Loops

JavaScript performance comparison

Revision 250 of this test case created by James

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
  var a = new Array(1000);
  var len = a.length;
  for(var i=-1;++i<len;) a[i]=i;

};
</script>

Preparation code output

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
for loop set length
for (var i = 0, n = len; i < n; ++i) {
  if (typeof(a[i]) === "undefined")
    throw "error";
}
pending…
for reverse loop
for (var i = len - 1; i >= 0; --i) {
  if (typeof(a[i]) === "undefined")
    throw "error";
}
pending…
for inc inline
for (var i = -1, n = len; ++i < n;) {
  if (typeof(a[i]) === "undefined")
    throw "error";
}
pending…
for reverse dec inline
for (var i = len; --i >= 0;) {
  if (typeof(a[i]) === "undefined")
    throw "error";
}
pending…
for ++ Before
for (var i = 0; i < len; ++i) {
  if (typeof(a[i]) === "undefined")
    throw "error";
}
pending…
for ++ After
for (var i = 0; i < len; i++) {
  if (typeof(a[i]) === "undefined")
    throw "error";
}
pending…
for ++ Before & Define
var i;
for (i = 0; i < len; ++i) {
  if (typeof(a[i]) === "undefined")
    throw "error";
}
pending…
for ++ After & Define
var i;
for (i = 0; i < len; i++) {
  if (typeof(a[i]) === "undefined")
    throw "error";
}
pending…
while reverse dec inline
var i = len;
while (i-- > 0) {
  if (typeof(a[i]) === "undefined")
    throw "error";
}
pending…
while reverse dec inline implicit
var i = len;
while (i--) {
  if (typeof(a[i]) === "undefined")
    throw "error";
}
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