flatMap vs reduce

Benchmark created on


Setup

const count = 10;
const items = [];
for (i=0; i < count; i++) {
  items.push({
  	text: 'See How',
    href: 'https://some.url.com',
    image: i % 3 ? '' : 'ABAQAAAQABAAD/2wBDAAYE'
  });
}

Test runner

Ready to run.

Testing in
TestOps/sec
flatMap
items.flatMap((el) =>
  el.image.trim().length
  ? { ...el, data: 'blah,'.concat(el.image) }
  : []
);
ready
reduce
items.reduce((acc, cur) => {
  if (cur.image.trim().length) {
  	acc.push({
  	  ...cur,
  	  data: 'blah,'.concat(cur.image)
  	});
  }
  return acc;
}, []);
ready

Revisions

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