array vs function

JavaScript performance comparison

Test case created

Preparation code

<script>
  tilesArray1 = [];
  tilesArray1.push([
    ["1"]
  ]);
 
  tilesArray2 = []
  tilesArray2.push({
    position: [1, 1, 1]
  });
 
  var X = 0,
      Y = 0,
      Z = 0;
</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
array
var tile = tilesArray1[X][Y][Z];
pending…
object
function getTile(x, y, z) {

  //loop through the tiles till we find the right one
  for (var i = 0; i < tilesArray2.length; i += 1) {

    //grab the tile
    var tile = tilesArray[i];

    //check the tile position to see if it is the one requested
    if (tile.position[0] = x && tile.position[1] = y && tile.position[2] = z) {
      return tile;
    }
  }

  //if the tile is not found and we fall out of the for loop return false
  return false;
}
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