From f474ba28a5a46f39dd51e9d1838c18f07cd14a3d Mon Sep 17 00:00:00 2001 From: "(quasar) nebula" Date: Wed, 13 Jul 2022 21:07:29 -0300 Subject: optimize sortByName a little --- src/util/wiki-data.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'src/util') diff --git a/src/util/wiki-data.js b/src/util/wiki-data.js index 3e564b96..97ffab76 100644 --- a/src/util/wiki-data.js +++ b/src/util/wiki-data.js @@ -152,14 +152,25 @@ export function sortByDirectory( } export function sortByName(data, {getName = (o) => o.name} = {}) { + const nameMap = new Map(); + const normalizedNameMap = new Map(); + for (const o of data) { + const name = getName(o); + const normalizedName = normalizeName(name); + nameMap.set(o, name); + normalizedNameMap.set(o, normalizedName); + } + return data.sort((a, b) => { - const an = getName(a); - const bn = getName(b); - const ann = normalizeName(an); - const bnn = normalizeName(bn); - return ( - compareCaseLessSensitive(ann, bnn) || compareCaseLessSensitive(an, bn) - ); + const ann = normalizedNameMap.get(a); + const bnn = normalizedNameMap.get(b); + const comparison = compareCaseLessSensitive(ann, bnn); + if (comparison !== 0) + return comparison; + + const an = nameMap.get(a); + const bn = nameMap.get(b); + return compareCaseLessSensitive(an, bn); }); } -- cgit 1.3.0-6-gf8a5