array vs set (v9)

Revision 9 of this benchmark created on


Setup

function generateContacts(n) {
	return [...Array(n)].map((_, i) => ({id: Math.floor(Math.random() * n), name: `joe #${i}`}));
}
function sample(arr, n) {
  const sampledElements = [];
  for (let i = 0; i < n; i++) {
    const randomIndex = Math.floor(Math.random() * arr.length);
    sampledElements.push(arr[randomIndex].id);
  }
  return sampledElements;
}
const allContacts = generateContacts(10000);
const selectedContactIds = sample(allContacts, 100);

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.