Find vs Map

Benchmark created on


Setup

// Setup
let specs = [];
let result = [];
let size = 1000; // adjust to 1000, 5000, etc.

for (let i = 0; i < size; i++) {
  let ref = Math.random().toString(36).substring(2, 15);
  specs.push({ reference: ref });
  result.push({ reference: ref, data: "data-" + i });
}

// Shuffle specs randomly
specs.sort(() => Math.random() - 0.5);

Teardown

specs = [];
result = []

Test runner

Ready to run.

Testing in
TestOps/sec
Find
for (const spec of specs) {
  const ticketTemplate = result.find(function(it) {
    return it.reference === spec.reference;
  });

  if (ticketTemplate) {
    continue
  }
}
ready
Map
let map = new Map(result.map(function(r) {
  return [r.reference, r];
}));

for (let spec of specs) {
  let ticketTemplate = map.get(spec.reference);

  if (ticketTemplate) {
    continue
  }
}
ready

Revisions

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