Skip to content

Commit 6cc07b4

Browse files
committed
perf: move sort order callback outside loop
1 parent 81f252b commit 6cc07b4

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

site/src/api/queries/groups.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,21 @@ export function groupsByUserId(organizationId: string) {
4545
}
4646
}
4747

48-
for (const groupsList of userIdMapper.values()) {
49-
groupsList.sort((g1, g2) => {
50-
const key =
51-
g1.display_name && g2.display_name ? "display_name" : "name";
48+
// Defined outside the loop because it can be reused by all group arrays,
49+
// and doesn't need to be rebuilt from scratch on each iteration
50+
const orderGroupsByName = (g1: Group, g2: Group) => {
51+
const key =
52+
g1.display_name && g2.display_name ? "display_name" : "name";
53+
54+
if (g1[key] === g2[key]) {
55+
return 0;
56+
}
5257

53-
if (g1[key] === g2[key]) {
54-
return 0;
55-
}
58+
return g1[key] < g2[key] ? -1 : 1;
59+
};
5660

57-
return g1[key] < g2[key] ? -1 : 1;
58-
});
61+
for (const groupsList of userIdMapper.values()) {
62+
groupsList.sort(orderGroupsByName);
5963
}
6064

6165
return userIdMapper as GroupsByUserId;

0 commit comments

Comments
 (0)