Skip to content

Commit

Permalink
Merge pull request #1801 from matrix-org/gsouquet/localecompare-perf
Browse files Browse the repository at this point in the history
Improve calculateRoomName performances by using Intl.Collator
  • Loading branch information
germain-gg authored Jul 21, 2021
2 parents a878588 + f708c47 commit e85038e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/models/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2053,7 +2053,7 @@ export class Room extends EventEmitter {
(m.membership === "invite" || m.membership === "join");
});
// make sure members have stable order
otherMembers.sort((a, b) => a.userId.localeCompare(b.userId));
otherMembers.sort((a, b) => utils.compare(a.userId, b.userId));
// only 5 first members, immitate summaryHeroes
otherMembers = otherMembers.slice(0, 5);
otherNames = otherMembers.map((m) => m.name);
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,3 +683,13 @@ export function lexicographicCompare(a: string, b: string): number {
// hidden the operation in this function.
return (a < b) ? -1 : ((a === b) ? 0 : 1);
}

const collator = new Intl.Collator();
/**
* Performant language-sensitive string comparison
* @param a the first string to compare
* @param b the second string to compare
*/
export function compare(a: string, b: string): number {
return collator.compare(a, b);
}

0 comments on commit e85038e

Please sign in to comment.