Array.includes vs Set.has for random IDs

Benchmark created on


Setup

const id = () => (Math.random() + 1).toString(36).substring(2);

const count = 300;

const arr = Array(count).fill().map(id);
const set = new Set(arr);

// Create an agenda of 50 IDs that exist in the set,
// and 50 that don't.
const toFind = [
  ...arr.slice(0, count/2),
  ...Array(count/2).fill().map(id)
]

Test runner

Ready to run.

Testing in
TestOps/sec
Array.includes
toFind.forEach(id => arr.includes(id));
ready
Set.has
toFind.forEach(id => set.has(id));
ready

Revisions

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