2D map structure

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function Tile(x, y) {
      this.x = x; this.y = y;
    }
   
    var stringMap = {}
    var arrayMap = {}
    var arrayArray = []
   
    var tiles = [];
    for(var i = 0; i < 1000; ++i) {
      var x = Math.floor(Math.random() * 500);
      var y = Math.floor(Math.random() * 500);
      var tile = new Tile(x, y);
      tiles.push(tile);
   
      stringMap["" + x + " " + y] = tile;
      arrayMap[[x, y]] = tile;
      arrayArray[x] = arrayArray[x] || []; arrayArray[x][y] = tile;
    }
   
    positiveCoord = tiles[500];
    negativeCoord = {x: 502, y: 300};
   
    function testStringMap(x, y) {
      return stringMap["" + x + " " + y];
    }
   
    function testArrayMap(x, y) {
      return arrayMap[[x, y]];
    }
   
    function testArrayArray(x, y) {
      var col;
      if(col = arrayArray[x]) {
        return col[y];
      }
    }
};
</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
String map
testStringMap(positiveCoord.x, positiveCoord.y);
testStringMap(negativeCoord.x, negativeCoord.y);
pending…
Array map
testArrayMap(positiveCoord.x, positiveCoord.y);
testArrayMap(negativeCoord.x, negativeCoord.y);
pending…
Array array
testArrayArray(positiveCoord.x, positiveCoord.y);
testArrayArray(negativeCoord.x, negativeCoord.y);
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