Object.groupBy()

Benchmark created on


Setup

const users = [
    { name: 'Alice', gentle: 'female' },
    { name: 'Bob', gentle: 'male' },
    { name: 'Charlie', gentle: 'male' },
    { name: 'Eve', gentle: 'female' },
];

Test runner

Ready to run.

Testing in
TestOps/sec
for ... of
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;
}
const groupByAge = groupBy(users, user => user.gentle);
ready
Object.groupBy()
const groupByAge = Object.groupBy(users, user => user.gentle);
ready

Revisions

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