groupBy: reduce vs for vs Object.groupBy (v2)

Revision 2 of this benchmark created on


Setup

const keys = ["hello", "world", "wow", "this", "is", "fun"];

const values = [];
for (let i = 0; i < 100_000; i++) {
  values.push([keys[Math.floor(Math.random() * keys.length)], i]);
}

function groupByFor(iter) {
  const result = {};
  for (const value of iter) {
    const entry = (result[value[0]] ||= []);
    entry.push(value);
  }
  return result;
}

function groupByReduce(iter) {
  return iter.reduce((result, value) => {
    const entry = (result[value[0]] ||= []);
    entry.push(value);
    return result
  }, {});
}

Test runner

Ready to run.

Testing in
TestOps/sec
reduce
groupByReduce(values)
ready
for loop
groupByFor(values)
ready
Object.groupBy
Object.groupBy(values, v => v[0])
ready

Revisions

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