« get me outta code hell

optimize sortByName a little - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
author(quasar) nebula <qznebula@protonmail.com>2022-07-13 21:07:29 -0300
committer(quasar) nebula <qznebula@protonmail.com>2022-07-13 21:07:29 -0300
commitf474ba28a5a46f39dd51e9d1838c18f07cd14a3d (patch)
tree32a716fbd348b03876ff2d9eeaaccec25e43945f /src/util
parentbc3bc359904ae75cd27854192e2739db6ea126bf (diff)
optimize sortByName a little
Diffstat (limited to 'src/util')
-rw-r--r--src/util/wiki-data.js25
1 files changed, 18 insertions, 7 deletions
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);
   });
 }