array vs set (v7)

Revision 7 of this benchmark created on


Setup

function generateContacts(size) {
	return [...Array(size)].map((_, i) => ({id: Math.floor(Math.random() * size), name: `jane doe #${i}`}));
}
function sample(arr) {
  const sampledElements = [];
  for (let i = 0; i < 100; i++) {
    const randomIndex = Math.floor(Math.random() * arr.length);
    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.