You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Nov 27, 2023. It is now read-only.
Array grouping is an extremely common operation, best exemplified by
SQL's GROUP BY clause and MapReduce programming (which is better
thought of map-group-reduce). The ability to combine like data into
groups allows developers to compute higher order datasets, like the
average age of a cohort or daily LCP values for a webpage.
Two methods are offered, Object.groupBy and Map.groupBy. The first
returns a null-prototype object, which allows ergonomic destructuring
and prevents accidental collisions with global Object properties. The
second returns a regular Map instance, which allows grouping on
complex key types (imagine a compound key or tuple).
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-patches Array.prototype with an incompatible
method. By providing a native groupBy, 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.