Run details:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Chrome 131
Linux
Desktop
8 days ago
Test name Executions per second
Map lookup 397.2 Ops/sec
Array lookup 15354.5 Ops/sec
Script Preparation code:
x
 
var map = new Map();
var arrLookup = [];
var items = [];
for (var i = 0; i < 100; i++) {
    items[i] = [];
    for (var j = 0; j < 100; j++) {
        items[i][j] = [i,j];
        if (Math.random() < 0.1) {
            map.set(i+","+j, Math.random()>=0.5);
            if (!arrLookup[i]) arrLookup[i] = [];
            arrLookup[i][j] = Math.random()>=0.5;
        }
    }
}
Tests:
  • Map lookup

     
    var results = [], i, j;
    for (i = 0; i < 100; i++) {
        for (j = 0; j < 100; j++) {
            if (map.has(items[i][j] +"")) results.push(map.get(items[i][j]+""));
        }
    }
    if (results.length === 0) throw Error();
  • Array lookup

     
    var results = [], i, j;
    for (i = 0; i < 100; i++) {
        for (j = 0; j < 100; j++) {
            if (arrLookup[i] && typeof arrLookup[i][j] !== "undefined") results.push(arrLookup[i][j]);
        }
    }
    if (results.length === 0) throw Error();