groupBy

Benchmark created on


Setup

const arr = new Array(100000).fill(0).map(a => ({type:String.fromCharCode(Math.floor(Math.random()*88)+32), data: Math.random()}));

Test runner

Ready to run.

Testing in
TestOps/sec
Object.groupBy
const ogrby = Object.groupBy(arr, ({type}) => type)
ready
reduce no opt
const red = arr.reduce((group, product) => {
  const { type } = product;
  group[type] = group[type] ?? [];
  group[type].push(product);
  return group;
}, {});
ready
reduce opt
const redopt = arr.reduce((group, product) => {
  const { type } = product;
  group[type]?.push(product)?? (group[type] = [product]);
  return group;
}, {});
ready

Revisions

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