array vs set (v6)

Revision 6 of this benchmark created on


Setup

function generateContacts(size) {
	return [...Array(size)].map((_, i) => ({id: Math.floor(Math.random() * size * 100), name: `jane doe #${i}`}));
}

function sample(arr) {
  const sampledElements = [];
  const totalElements = arr.length;

  for (let i = 0; i < 100; i++) {
    const randomIndex = Math.floor(Math.random() * totalElements);
    sampledElements.push(arr[randomIndex].id);
  }

  return sampledElements;
}

const allContacts = generateContacts(10000);
const selectedContactIds = sample(allContacts);

Test runner

Ready to run.

Testing in
TestOps/sec
array
const selected = allContacts.filter((c) => selectedContactIds.includes(c.id));
ready
set
const selectedSet = new Set(selectedContactIds);

const selected = allContacts.filter((c) => selectedSet.has(c.id));
ready

Revisions

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