For Loop

JavaScript performance comparison

Revision 74 of this test case created by tomByrer

Info

v74: removed all 'each' commands; performance is dismal & clogged up testing. Really folks, over 6 tests is alot, 17 over the top.

Preparation code

<script src="////cdnjs.cloudflare.com/ajax/libs/lodash.js/1.2.0/lodash.min.js">
</script>
<script type="text/javascript">
  var arr = _.range(1,1000);
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
while imitating for
var i = 0;
while (i < arr.length) {
  arr[i]=i;
  i++;
};
pending…
while imitating for loop, caching the length
var i = 0,
    len = arr.length;
while (i < len) {
  arr[i]=i;
  i++;
};
pending…
Reverse while
var i = arr.length;
while (i--) {
  arr[i]=i;
};
pending…
Rev while w/o implicit ToBoolean
var i = arr.length;
while (i-- > 0) {
  arr[i]=i;
};
pending…
Reverse do … while
var i = arr.length;
do {
  arr[i]=i;
} while (i--);
pending…
Reverse for
for (var i = arr.length; i--;) {
  arr[i]=i;
};
pending…
for
for (var i = 0; i < arr.length; ++i) {
  arr[i]=i;
};
pending…
for, length cached
for (var i = 0, len = arr.length; i < len; ++i) {
  arr[i]=i;
};
pending…
Cool guy 'for'
for (var i = -1; ++i < arr.length;) {
  arr[i]=i;
};
pending…
Cool guy, length cached
for (var i = -1, len = arr.length; ++i < len;) {
  arr[i]=i;
};
pending…
For += 1
var i;
for (i = 0; i < arr.length; i += 1) {
  arr[i]=i;
}
pending…
Loop case 2
var i, l;
for (i = 0, l = arr.length; i < l; i += 1) {
  arr[i]=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