reduce vs. filter/map/set

Benchmark created on


Setup

const selectedItems = Array.from({ length: 10000 }, (_, i) => ({
  id: i % 3 === 0 ? undefined : i
}));

Test runner

Ready to run.

Testing in
TestOps/sec
filter + map + Set
new Set(
  selectedItems
    .filter((item) => item.id !== undefined)
    .map((item) => String(item.id))
);
ready
reduce + Set
selectedItems.reduce((set, item) => {
  if (item.id !== undefined) set.add(String(item.id));
  return set;
}, new Set());
ready

Revisions

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