The MDN docs state:
Note: In some versions of some browsers, this method was implemented as the method Array.prototype.group(). Due to web compatibility issues, it is now implemented as a static method.
However, similar issues arose with flat
(see SmooshGate) but this was resolved by renaming the term to flatten
. Why didn't TC39 take the same approach with groupBy
and find a non-conflicting name?
The original proposal states the reasoning:
Why static methods?
We've found a web compatibility issue with the name
Array.prototype.groupBy
. The Sugar library until v1.4.0 conditionally monkey-patchesArray.prototype
with an incompatible method. By providing a nativegroupBy
, these versions of Sugar would fail to install their implementation, and any sites that depend on their behavior would break. We've found some 660 origins that use these versions of the Sugar library.We then attempted the name
Array.prototype.group
, but this ran into code that uses an array as an arbitrary hashmap. Because these bugs are exceptionally difficult to detect (it requires devs to detect and know how to report the bug to us), the committee didn't want to attempt another prototype method name. Instead we chose to use static method, which we believe is unorthodox enough to not risk a web compatibility issue. This also gives us a nice way to support Records and Tuples in the future.https://github.com/tc39/proposal-array-grouping?tab=readme-ov-file#why-static-methods