Is Javascript Object.groupBy() worth the hype? let’s find out

is-javascript-object.groupby()-worth-the-hype?-let’s-find-out

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

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

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

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

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 🙂

javascript #nodejs #benchmark

Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
html-can-do-this?-part-1

HTML can do this? Part 1

Next Post
a-arte-do-erro-sem-mensagem:-por-que-meu-codigo-python-interpretou-minha-variavel-como-tupla-ao-inves-de-int

A arte do erro sem mensagem: por que meu código Python interpretou minha variável como tupla ao invés de int

Related Posts