Coords storage

Benchmark created on


Setup

const data = [];
const n_axis = 2000;

let i = 0;
for (x = 0; x < n_axis; x++) {
  if (x % 2) {
    continue;
  }
  for (y = 0; y < n_axis; y++) {
    if (!(y % 2)) {
      continue;
    }
    const ids = Array(((x + y) % 3) || 1)
      .fill(null)
      .map(() => i++);
    data.push([x, y, ids]);
  }
}
const coords = {};

Test runner

Ready to run.

Testing in
TestOps/sec
nested objects operador in (only check)
function check([x, y]) {
  return x in coords && y in coords[x];
}

data.forEach(([x, y, ids]) => {
  coords[x] = coords[x] || {};
  coords[x][y] = ids;
});

for(x = 0; x < n_axis; x++) {
    for(y = 0; y < n_axis; y++) {
        check([x,y])
    }
}
ready
nested objects direct access (only check)
function check([x, y]) {
  return !!coords[x]?.[y];
}

data.forEach(([x, y, ids]) => {
  coords[x] = coords[x] || {};
  coords[x][y] = ids;
});

for(x = 0; x < n_axis; x++) {
    for(y = 0; y < n_axis; y++) {
        check([x,y])
    }
}
ready
combined string key
function check([x, y]) {
  return !!coords[x+'.'+y];
}

data.forEach(([x, y, ids]) => {
  coords[x+'.'+y] = ids;
});

for(x = 0; x < n_axis; x++) {
    for(y = 0; y < n_axis; y++) {
        check([x,y])
    }
}
ready
combined string key template literal
function check([x, y]) {
  return !!coords[`${x}.${y}`];
}

data.forEach(([x, y, ids]) => {
  coords[`${x}.${y}`] = ids;
});

for(x = 0; x < n_axis; x++) {
    for(y = 0; y < n_axis; y++) {
        check([x,y])
    }
}
ready
nested objects direct access
function check([x, y]) {
  return coords[x]?.[y];
}

data.forEach(([x, y, ids]) => {
  coords[x] = coords[x] || {};
  coords[x][y] = ids;
});

for(x = 0; x < n_axis; x++) {
    for(y = 0; y < n_axis; y++) {
        check([x,y])
    }
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.