Large Object Versus Separate Objects (with Manual Look-ups)

JavaScript performance comparison

Test case created by Vaughn

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    //For test #1 - Separate Objects
    tile = [];
    for (x = 0; x <= 256; x++) {
      for (y = 0; y <= 256; y++) {
        tile[x] = tile[x] || {};
        tile[x][y] = "test" + Math.floor(Math.random() * 10);
      }
    }
   
    monster = [];
    for (i = 0; i <= 1000; i++) {
      monster[i] = {
        x: Math.floor(Math.random() * 256 + 1),
        y: Math.floor(Math.random() * 256 + 1)
      };
    }
   
    //For test #2 - Combined Object
    tile2 = [];
    for (x = 0; x <= 256; x++) {
      for (y = 0; y <= 256; y++) {
        tile2[x] = tile[x] || {};
        tile2[x][y] = {
          tile: "test" + Math.floor(Math.random() * 10),
          monsters: []
        };
      }
    }
   
    for (i = 0; i <= 1000; i++) {
      tile2[Math.floor(Math.random() * 256 + 1)][Math.floor(Math.random() * 256 + 1)].monsters.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
Separate Objects
var tempvar = [];

for (var i in monster) {
  tempvar[monster[i].x] = tempvar[monster[i].x] || {};
  tempvar[monster[i].x][monster[i].y] = i;
}

for (x = 0; x <= 16; x++) {
  for (y = 0; y <= 16; y++) {
    if (tile[x] && tile[x][y]) {
      //
    }
    if (tempvar[x] && tempvar[x][y]) {
      //
    }
  }
}
pending…
Combined Object
for (x = 0; x <= 16; x++) {
  for (y = 0; y <= 16; y++) {
    if (tile2[x] && tile2[x][y]) {
      //
      if (tile2[x][y].monsters) {
        //
      }
    }
  }
}
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