News for hackers. Stuff that matters.
Re:TC39: Add Object.groupBy and Map.groupBy by carterschonwald on Tuesday December 19, 2023 @04:32PM (#38701948)
This seems like a nice addition. Is it soemthing that a lot of tools roll their own, or is something that can’t be done in user space? I’m relatively ignorant of the limits of js
Re:TC39: Add Object.groupBy and Map.groupBy by tibbar on Tuesday December 19, 2023 @04:35PM (#38701984)
It's fairly easy to roll your own. An example implementation of groupBy in lodash, for example: https://github.com/lodash/lodash/blob/4.17.15/lodash.js#L939....

More broadly, this is an example of a utility function for organizing data in a data structure, which JavaScript and all other major programming languages are well-equipped to implement in arbitrary ways, so this is just a convenience function. It's not like a hook into underlying environment APIs that can't be independently shimmed.

Re:TC39: Add Object.groupBy and Map.groupBy by ricardobeat on Tuesday December 19, 2023 @04:56PM (#38702227)
It's very funny to point to lodash as an example of simplicity. Here is what happens when you start unraveling the code you linked to:

https://gist.github.com/ricardobeat/040af80971273f5abd71c2bf...

I got tired, but wouldn't be surprised if the whole thing goes beyond 1000 lines. It's basically a black hole. Better hope you really, really trust their test suite, it's impossible to debug and there must only be a handful of people familiar with the whole codebase.

Re:TC39: Add Object.groupBy and Map.groupBy by jauntywundrkind on Tuesday December 19, 2023 @04:42PM (#38702072)
Trying to read lodash can (to put it mildly) be difficult. Lots of code reuse.

This is probably missing checking for a bunch of corner cases, but this is the general idea:

  // quick & dirty Object.groupBy
  function groupBy(iterable, callbackFn) {
    const out = {}
    for(const [key, value] in Object.entries(iterable)) {
      const name = callbackFn(value, key)
      (out[name] || (out[name] = [])).push(value)
    }
    return out
  }
I literally wrote something very like this yesterday (after checking & finding it only got added to Node 21, and deciding not to pull in a dependency).

Edit: had keyBy impl in here, fixed! Thanks commenters!

Re:TC39: Add Object.groupBy and Map.groupBy by tibbar on Tuesday December 19, 2023 @04:44PM (#38702094)
I think `out` should be typed as a map to an array in this case; each iteration should be appending to the corresponding array instead of overwriting the entire entry.
Re:TC39: Add Object.groupBy and Map.groupBy by moritzwarhier on Tuesday December 19, 2023 @04:50PM (#38702170)
Oh yes, I agree.

I remember me running into the problem of internal dependencies when extracting parts of it into a non-ESM/non-build project some years ago...

Like, the internal consistency of the library probably benefits, and with tree-shaking it works reasonably well but it is still a poster example of DRY vs KISS...

It has its benefits, but its API surface can be annoying, too. When I encounter it, I often have to think for a second and look at the docs, e.g. some cryptic xor or map function with three parameters, one of which being an optional config object.

OTOH, it can be great to have a well-tested implementation at hand for all kinds of higher-order functions, like throttle, debounce etc

Re:TC39: Add Object.groupBy and Map.groupBy by explaininjs on Tuesday December 19, 2023 @05:34PM (#38702834)
Aaannd… now you have a prototype pollution vulnerability :)

Object.from(null), always.

Re:TC39: Add Object.groupBy and Map.groupBy by quonn on Tuesday December 19, 2023 @04:48PM (#38702149)
That‘s closer to keyBy than groupBy.
Re:TC39: Add Object.groupBy and Map.groupBy by moritzwarhier on Tuesday December 19, 2023 @04:39PM (#38702039)
It is indeed something that you often roll your own, write an utility function for, or include libraries like lodash for.

JS standard library is notoriously deficient.

This is a good addition in my view too, especially including Map.

When dealing with DB/API results, one can of course always say that such grouping in the client is an anti-pattern.

But in reality it can be reasonable and useful.

If you write one-off algorithms in JS to transform small amounts of data on the client, you probably habe done this.

And even for data-heavy applications, it could prove to be a performance benefit for functions that use Map instances during computation.

Though that's just wishful thinking until the runtime developers choose to optimize these functions, if possible.

Re:TC39: Add Object.groupBy and Map.groupBy by pmontra on Tuesday December 19, 2023 @04:48PM (#38702145)
> When dealing with DB/API results, one can of course always say that such grouping in the client is an anti-pattern.

It depends on how many data you're working on. This groupBy is useful for filters in data tables.

If you have to download a zillion of records and filter them, client side filtering is a bad idea. If you have a few thousands of them, it could possibly be the faster solution overall.

Re:TC39: Add Object.groupBy and Map.groupBy by moritzwarhier on Tuesday December 19, 2023 @05:58PM (#38703122)
Exactly.
Re:TC39: Add Object.groupBy and Map.groupBy by thenbe on Tuesday December 19, 2023 @05:39PM (#38702887)
You can roll your own or use a utility library. A simple zero-dependency library would be something like just-* [1]. Although I now prefer remeda [2] as it seems to have the best typescript support, especially the strict variants such as `groupBy.strict`.

[1] https://github.com/angus-c/just#just-group-by

[2] https://remedajs.com/docs#groupBy

Re:TC39: Add Object.groupBy and Map.groupBy by paavohtl on Tuesday December 19, 2023 @04:36PM (#38702001)
It can absolutely be written in JavaScript (it is a Turing complete language after all), and it is a common utility function found in most general utility libraries like Lodash and Ramda.
Re:TC39: Add Object.groupBy and Map.groupBy by erulabs on Tuesday December 19, 2023 @04:35PM (#38701987)
It can be trivially rolled by hand, but it’s also a pretty common function in other languages. I’d guess most node programmers reach for lodash for this.
Re:TC39: Add Object.groupBy and Map.groupBy by paulddraper on Tuesday December 19, 2023 @04:48PM (#38702158)
> a pretty common function in other languages

Except oddities like C, C++, Perl, PHP, and Go :)

Re:TC39: Add Object.groupBy and Map.groupBy by erulabs on Tuesday December 19, 2023 @05:12PM (#38702527)
Oh PHP programmers would just rely on MySQL's groupBy, we both know it! :P