Is Javascript Object.groupBy() worth the hype? let's find out
Tags: node, javascript, benchmark
Published: 2023-12-16 18:42:28

With the upcoming release of NodeJS 21, we're getting an exciting new method that should make our object groupings more straightforward: `Object.groupBy()`.

As the name of the method suggests, this method accepts an array and groups it by a parameter via a callback function. The use of this new method looks like this:

```
const people =[
  {"first_name":"Dotti","birth_year":1985},
  {"first_name":"Gratia","birth_year":1992},
  {"first_name":"Robert","birth_year":1985},
  {"last_name":"Versey","birth_year":1992}
]

const peopleGroupedByBirthYear = Object.groupBy(people, (person) => person.birth_year)

console.log(peopleGroupedByBirthYear)
/*
{
  "1985":[
    {"first_name":"Dotti","birth_year":1985},
    {"first_name":"Robert","birth_year":1985}
  ],
  "1992":[
    {"first_name":"Gratia","birth_year":1992},
    {"last_name":"Versey","birth_year":1992}
  ]
}
*/
```

But does this method bring us more value except for syntactic sugar? Does it improve runtime and increase performance? Let's take a deeper look.

## Technicalities
To test this new functionality with different sets and data scale sizes, I've prepared datasets with **1k users**, **10k users**, **100k users**, and **1m users**. Each user within those datasets has been given a birth year between 1900 and 2000 to make the groups a bit larger.

For the benchmark, I used mostly common loops and array methods, to see which one will shine the brightest.

Additionally, every function underwent multiple individual tests across various datasets on a **Docker container** equipped with a **single-core CPU and 512 MB of RAM**. This setup ensured that my machine's performance did not influence the results. The functions were repeatedly tested to calculate a cumulative average runtime. Now, let's explore some charts based on these evaluations.

![1k users results](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gsvu4mrb1tk5gdp62q54.png)


On a dataset with 1k users, we can see that `Object.groupBy()` is indeed the faster one, surpassing other implementations for group data (which is pretty neat, to be honest). I like it, but will it outstand larger datasets? Let's find out:

![10k users result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dinttoww2tivh7eh6k1j.png)


As for 10k users, `Object.groupBy()` starts really to shine, and it beats other iterations and functions by around 50% on average (really nice!), but is it **blazingly fast**?

![100k users result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l10xmbwfs8sn9h1v3r95.png)


On a dataset with 100k users, we're starting to see that `Object.groupBy()` is starting to run out of fuel, but it still outshines the other functionalities and is superior to other implementations! But is it going to win the race with a dataset of 1 million users?

![1m users results](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2270skz079yeyxx5nlew.png)


Ouch, it seems that `Object.groupBy()` is starting to fall behind when it comes to larger datasets, with Reduce taking its place. Why is that what you're asking? Well, that's for another article.



## Conclusion
`Object.groupBy()` looks to be a promising new (_and almost blazingly fast_) functionality for small to medium-sized datasets, which is neat! For the average use case, you will find this new feature to improve your existing code base. As for larger datasets, that's not the case.



Tell me what you think in the comments below :) 

https://github.com/svidlak/groupby-benchmark
(1million file is zipped because it exceeds github's filesize policy)