arrays vs lookup tables

Benchmark created on


Setup

// Sizes per request
const N_ITEMS = 100;
const ROWS   = 100;
const COLS   = 50;

// Data: items with ids 0..99
const items = Array.from({ length: N_ITEMS }, (_, i) => ({ id: i, value: i * i }));

// A 100x50 grid of random ids (0..99)
const grid = Array.from({ length: ROWS }, () =>
  Array.from({ length: COLS }, () => (Math.random() * N_ITEMS) | 0)
);

// Lookup tables
const byIdObj = Object.fromEntries(items.map(it => [it.id, it]));
const byIdMap = new Map(items.map(it => [it.id, it]));

// Prevent dead-code elimination
let checksum = 0;
function consume(v) { checksum = (checksum + (v?.value ?? 0)) | 0; }

Test runner

Ready to run.

Testing in
TestOps/sec
.find over 100 items (5,000 times)
checksum = 0;
for (let r = 0; r < ROWS; r++) {
  const row = grid[r];
  for (let c = 0; c < COLS; c++) {
    const id = row[c];
    const found = items.find(it => it.id === id);
    consume(found);
  }
}
checksum;
ready
Object lookup table
checksum = 0;
for (let r = 0; r < ROWS; r++) {
  const row = grid[r];
  for (let c = 0; c < COLS; c++) {
    const id = row[c];
    const found = byIdObj[id];
    consume(found);
  }
}
checksum;
ready

Revisions

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