fastest while loops

JavaScript performance comparison

Revision 2 of this test case created

Info

Forked from Fastest array loops in Javascript #32, where 'while' loops far outstripped 'for' loops. Let's see if we can speed up while loops!

Preparation code

<script>
  // Populate the base array
  var arr = [];
  for (var i = 0; i < 1000; i++) {
    arr[i] = i;
  }

  //called inside loops
  function someFn(ix) {
    return ix * 5 + 1 / 3 * 8;
  }
</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
While len--
var l = arr.length;
while(l--) { //counts backwards from max to 0, assumes dense array
  someFn(arr[l]);
}
pending…
Ordered while len--
var l = arr.length,
    i = l - 1;
while(l--) {
  someFn(arr[i-l]); //now loop is from bottom-up
}
pending…
While --len
var l = arr.length;
while(--l) { //counting backwards with backwards notation
  someFn(arr[l]);
}
pending…
For normal
var l = arr.length;
for(var i = 0; i < l; i++){
  someFn(arr[i]);
}
pending…
For backwards
var l = arr.length;
for(var i = l; i => 0; i--){
  someFn(arr[i]);
}
pending…
For backwards mod
var l = arr.length;
for(var i = l; i => 0; --i){
  someFn(arr[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