simple compare for Object.groupBy v.s. for...of (v2)

Revision 2 of this benchmark created on


Setup

const users = [
            { name: 'Alice', gentle: 'female' },
            { name: 'Bob', gentle: 'male' },
            { name: 'Charlie', gentle: 'male' },
            { name: 'Eve', gentle: 'female' }
        ];
        function groupBy(array, keySelector) {
              const group = {};
              for (const element of array) {
                  const key = keySelector(element);
                  if (!group[key]) {
                      group[key] = [];
                  }
                  group[key].push(element);
              }
              return group;
          }
          
          function groupBy2(array, predicate) {
  return array.reduce((acc, value, index, array) => {
    const key = predicate(value, index, array)
    if (!acc[key])
      acc[key] = []
    acc[key].push(value)
    return acc
  })
}

Test runner

Ready to run.

Testing in
TestOps/sec
for...of
const groupedByAge = groupBy(users, user => user.gentle);
ready
Object.groupBy
const groupedByAge = Object.groupBy(users, user => user.gentle);
ready
groupBy2
const groupedByAge = groupBy2(users, user => user.gentle);
ready

Revisions

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