Unique (using Set) test (v3)

Revision 3 of this benchmark created on


Setup

const iota = n => 
  Array.from(Array(n), (_, i) => i);
  
const KINDS = ["a", "b", "c", "d"];

const randomString = n => [...crypto.getRandomValues(new Uint8Array(n))]
    .map(String.fromCharCode)
    .join("");

const roll = n => Math.floor(Math.random() * n)

const toThing = id => {
  const name = randomString(12)
  const kind = KINDS[roll(KINDS.length)]

  return { id, name, kind } 
}

const things = iota(100000).map(toThing);

const selected = KINDS[roll(KINDS.length)];

Test runner

Ready to run.

Testing in
TestOps/sec
Reduce
things.reduce((acc, {id, kind}) => {
  if (kind === selected) {
  	acc.add(id);
  }
  return acc;
}, new Set())
ready
filter and map
new Set(things
  .filter(({kind}) => kind === selected)
  .map(({id}) => id))
ready
flatMap
new Set(things
  .flatMap(({kind, id}) => 
    kind === selected ? [id] : []
  ))
ready
Set(reduce)
new Set(things.reduce((acc, {id, kind}) => {
  if (kind === selected) {
  	acc.push(id);
  }
  return acc;
}, []))
ready

Revisions

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