From f708c473851093a149de669e49e67ead6d18e553 Mon Sep 17 00:00:00 2001 From: Germain Souquet Date: Wed, 21 Jul 2021 14:18:22 +0200 Subject: [PATCH] Improve calculateRoomName performances by using Intl.Collator --- src/models/room.ts | 2 +- src/utils.ts | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/models/room.ts b/src/models/room.ts index 089e60fa8a7..9e14d190758 100644 --- a/src/models/room.ts +++ b/src/models/room.ts @@ -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); diff --git a/src/utils.ts b/src/utils.ts index a392006ea7e..4ab08f8b750 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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); +}