De-duping 1,000,000 items

Benchmark created on


Setup

const a = [];

for (let i = 0; i < 1000000; i++) {
  a.push({
    index: i,
    duplicationKey: `Element ${i % 300000}`
  });
}

// Example dedupe functionality; get an average
function dedupe(items) {
  const sum = items.reduce((acc, i) => acc + i.index, 0);
  const average = sum / items.length;

  return {
    ...items[0],
    average
  };
}

Test runner

Ready to run.

Testing in
TestOps/sec
loop - values - map
// Test case 1

const byKeyOne = {};

for (const element of a) {
  const current = byKeyOne[element.duplicationKey] ?? [];
  byKeyOne[element.duplicationKey] = [...current, element];
}

const output = Object.values(byKeyOne).map((items) => dedupe(items));

console.log(output.length);

ready
loop - values
// Test case 2

const byKeyTwo = {};

for (const element of a) {
  const current = byKeyTwo[element.duplicationKey] ?? [];
  byKeyTwo[element.duplicationKey] = [dedupe([...current, element])];
}

const output2 = Object.values(byKeyTwo);

console.log(output2.length);


ready
loop - values - no arrays
// Test case 3

const byKeyThree = {};

for (const element of a) {
  if (byKeyThree[element.duplicationKey]) {
    const group = byKeyThree[element.duplicationKey];
    group.sum += element.index;
    group.count += 1;
    group.average = group.sum / group.count;
  } else {
    byKeyThree[element.duplicationKey] = {
      ...element,
      sum: element.index,
      count: 1,
      average: element.index, // Initial average is the first element's index
    };
  }
}

const output3 = Object.values(byKeyThree);

console.log(output3.length);
ready

Revisions

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