Fastest Loop

JavaScript performance comparison

Revision 2 of this test case created by Hallvar Helleseth

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var t=0;
};
</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
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (var i = 0; i < a.length; i++) {t=a[i];}
pending…
For (cache length)
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (var i = 0, l = a.length; i < l; i++) {t=a[i];}
pending…
For (reverse)
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (var i = a.length; i--;) {t=a[i];}
pending…
For In
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (var i in a) {t=a[i];}
pending…
For (exists)
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (var i = 0; a[i]; i++) {t=a[i];}
pending…
While (imitate loop)
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var i = 0; while (i < a.length) { t=a[i]; i++; }
pending…
While (imitate loop) (cache length)
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var i = 0, l = a.length; while (i < l) { t=a[i]; i++; }
pending…
While (reverse)
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var i = a.length; while (i--) {t=a[i];}
pending…
Do While (reverse)
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var i = a.length - 1; do {t=a[i];} while (i--);
pending…
While (pop)
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// a.pop() will reduce the array to length 0 after each test run
var i; while (i = a.pop()) {t=i;}
pending…
.forEach()
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
a.forEach(function(i) {t=i;});
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