From d496569c9a86939d24bcaff4a891f8833b24f810 Mon Sep 17 00:00:00 2001 From: DominikDoom Date: Fri, 19 Jan 2024 20:17:14 +0100 Subject: [PATCH] Cache sort key for small performance increase --- javascript/_utils.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/javascript/_utils.js b/javascript/_utils.js index 6f454db..c967d2b 100644 --- a/javascript/_utils.js +++ b/javascript/_utils.js @@ -335,12 +335,19 @@ function getSortFunction() { let criterion = TAC_CFG.modelSortOrder || "Name"; const textSort = (a, b, reverse = false) => { - const textHolderA = a.type === ResultType.chant ? a.aliases : a.text; - const textHolderB = b.type === ResultType.chant ? b.aliases : b.text; + // Assign keys so next sort is faster + if (!a.sortKey) { + a.sortKey = a.type === ResultType.chant + ? a.aliases + : a.text; + } + if (!b.sortKey) { + b.sortKey = b.type === ResultType.chant + ? b.aliases + : b.text; + } - const aKey = a.sortKey || textHolderA; - const bKey = b.sortKey || textHolderB; - return reverse ? bKey.localeCompare(aKey) : aKey.localeCompare(bKey); + return reverse ? b.sortKey.localeCompare(a.sortKey) : a.sortKey.localeCompare(b.sortKey); } const numericSort = (a, b, reverse = false) => { const noKey = reverse ? "-1" : Number.MAX_SAFE_INTEGER;