浏览代码

Perf: improve sort speed / simplify

SukkaW 1 年之前
父节点
当前提交
5ec04e4728
共有 1 个文件被更改,包括 7 次插入10 次删除
  1. 7 10
      Build/lib/stable-sort-domain.ts

+ 7 - 10
Build/lib/stable-sort-domain.ts

@@ -1,14 +1,8 @@
 import type { PublicSuffixList } from '@gorhill/publicsuffixlist';
 import { sort } from 'timsort';
 
-const compare = (a: string | null, b: string | null) => {
+const compare = (a: string, b: string) => {
   if (a === b) return 0;
-  if (b == null) {
-    return 1;
-  }
-  if (a == null) {
-    return -1;
-  }
 
   const aLen = a.length;
   const r = aLen - b.length;
@@ -45,10 +39,13 @@ export const sortDomains = (inputs: string[], gorhill: PublicSuffixList) => {
   const sorter = (a: string, b: string) => {
     if (a === b) return 0;
 
-    const $a = domains.get(a) || a;
-    const $b = domains.get(b) || b;
+    const $a = domains.get(a);
+    const $b = domains.get(b);
 
-    return compare($a, $b) || compare(a, b);
+    if ($a && $b) {
+      return compare($a, $b) || compare(a, b);
+    }
+    return compare(a, b);
   };
 
   sort(inputs, sorter);