I’m building a data structure for storing tile data from isometric maps. I’m currently using a multidimensional array with 3 axis.
var tile = tilesArray[X][Y][Z];
I’m wondering if it would be faster to use a for loop to find data
tilesArray[i] = tileObject
function getTile(x, y, z) {
//loop through the tiles till we find the right one
for (var i = 0; i < tilesArray.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;
}
So to recap
var tile = tilesArray[X][Y][Z];
vs
var tile = getTile(x, y, z);
Benchmark
The object is 2/3 times as slow in chrome 12.
The object is 1000 times slower in firefox 4. (Basically arrays are really fast in FF4, about a 1000 times faster then chrome)
Becuase the Object is nice and OO and doesn’t involve having a 3 dimensional array with a LOT of gaps I would recommend using it as its just generally a better way to store data.