With the upcoming release of NodeJS 21, we're getting an exciting new method that should make our object groupings more straightforward: Object.gro...
For further actions, you may consider blocking this person and/or reporting abuse
I mean, I’ve been using maps in JS for years. groupBy is a convenience.
Well, now it's also a performance increase and not just syntactic sugar, which makes it even better
No doubt, but it’s nothing new.
I don't think there's any "hype" about
Object.groupBy
being super performant. It's just a convenience method. If it happens to be slightly more performant for some datasets, that's great, but I wouldn't rely on any performance gains being stable between different engines, different versions of the same engine, different hardware, or even necessarily different runs on the exact same setup.The key advantage is that it gives better ergonomics. Wanting to split an array into groups is a pretty common use case that didn't have an ergonomic way of doing it before now. For example, if you had a list of users that you wanted to group into admin and non-admin users, you either had to initialize variables for both then iterate with a for loop (a bit messy and verbose, especially with the array initialization in TypeScript), or you had to iterate through the array twice with
Array#filter
(wasteful as you only really wanted 1 iteration). Now it's simple:It's also pretty versatile — e.g. you can use it to segment an array into groups of a given size:
Actually, I needed this method this week & came upon this article, & discovered
groupBy
.Showing performance graphs without posting the code makes them hard to understand.
I got you covered @manchicken
github.com/svidlak/groupby-benchmark
Thanks! It’s always good to post the code with these things. Well-done.
My first article ever written so mistakes are unavoidable
No doubt. My advice: learn to love your mistakes.
We should also ask, if it´s worth to blow up the language core to infinitiy, just to add some convenience functions (see also this post). I would love this feature (if I ever used it), if it was part of a library. But too many features may also be confusing.
Well, every language has to go thru some kind of an evolution sometimes in order to improve itself, whenever its via internal optimization or via introduction of new API's
(the same as ES6 introduced us some functionalities to replace lodash / rambda dependencies, fatArrow functions),
Its important for JavaScript to keep on evolving and improving as the whole client-side ecosystem of JavaScript is evolving nonstop, so the language has to support those new needs.
I use it with lodash