MCPcopy Create free account
hub / github.com/nodejs/node / groupBy

Function groupBy

deps/v8/tools/system-analyzer/helper.mjs:28–50  ·  view source on GitHub ↗
(array, keyFunction, collect = false)

Source from the content-addressed store, hash-verified

26}
27
28export function groupBy(array, keyFunction, collect = false) {
29 if (array.length === 0) return [];
30 if (keyFunction === undefined) keyFunction = each => each;
31 const keyToGroup = new Map();
32 const groups = [];
33 const sharedEmptyArray = [];
34 let id = 0;
35 // This is performance critical, resorting to for-loop
36 for (let each of array) {
37 const key = keyFunction(each);
38 let group = keyToGroup.get(key);
39 if (group !== undefined) {
40 collect ? group.addEntry(each) : group.add();
41 continue;
42 }
43 let entries = collect ? [each] : sharedEmptyArray;
44 group = new Group(key, id++, array.length, entries, 1);
45 groups.push(group);
46 keyToGroup.set(key, group);
47 }
48 // Sort by length
49 return groups.sort((a, b) => b.length - a.length);
50}
51
52export function arrayEquals(left, right, compareFn) {
53 if (left == right) return true;

Callers 7

selectEntriesMethod · 0.90
getBreakdownMethod · 0.90
getBreakdownMethod · 0.90
constructorMethod · 0.90
_createMarkerNodeMethod · 0.90
_updateMethod · 0.90
createSubgroupsMethod · 0.90

Calls 6

sortMethod · 0.80
getMethod · 0.65
addMethod · 0.65
addEntryMethod · 0.45
pushMethod · 0.45
setMethod · 0.45

Tested by

no test coverage detected