MAX vs .length Loops

JavaScript performance comparison

Test case created by FH

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<div id="array-desc"></div>

<script>
var MAX_X = 10;
var MAX_Y = 10;

var a = new Array(MAX_X);
$("#array-desc").append("a.length = "+a.length+"<br/>");
for (var x=0; x<MAX_X; x++) {
  a[x] = new Array(MAX_Y - (x % 2));
  $("#array-desc").append("a["+x+"].length = "+a[x].length+"<br/>");
  for (var y=0; y<MAX_Y - (x % 2); y++) {
    a[x][y] = Math.random();
  }
}
</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
MAX (!= null)
for (var x=0; x<MAX_X; x++) {
  for (var y=0; y<MAX_Y; y++) {
    if (a[x][y] != null) {
      a[x][y] = 0;
    }
  }
}
 
pending…
.length
for (var x=0; x<a.length; x++) {
  for (var y=0; y<a[x].length; y++) {
    a[x][y] = 0;
  }
}
 
pending…
EXACT (calculated)
for (var x=0; x<MAX_X; x++) {
  for (var y=0; y<MAX_Y - (x % 2); y++) {
    a[x][y] = 0;
  }
}
 
pending…
.length (!= null)
for (var x=0; x<a.length; x++) {
  for (var y=0; y<a[x].length; y++) {
    if (a[x][y] != null) {
      a[x][y] = 0;
    }
  }
}
 
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