Self Interacting Loops

JavaScript performance comparison

Test case created by chris

Info

For large loops which require each element within the array to interact with all the other elements in array, what is the fastest way we can do that?

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    Array.prototype.clone = function(i) {
      return this.slice(i);
    };
    var arr = new Array();
    for (i = 0; i < 100; i++) arr.push(i);
};
</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, Cached
for (i = 0; k = arr.length - 1, i < k; i++) {
  var element1 = arr[i];
  for (u = i + 1; j = arr.length, u < j; u++) {
    var element2 = arr[u];
  }
}
pending…
While
var i = arr.length - 1;
while (i-- && i > 0) {
  var u = i;
  var element1 = arr[i];
  while (u--) {
    var element2 = arr[u];
  }
}
pending…
Pop
var i = arr.clone(0);
var element1, element2, u;
while (i.length > 0) {
  element1 = i.pop();
  u = i.length;
  while (u--) {
    var element2 = arr[u];
  }
}
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment