Surge_by_SukkaW/Build/lib/stable-sort-domain.ts
SukkaW e5d511d105 Perf: many changes
- Hoist process hosts line callback
- Reduce dp hosts file size
- Reduce domain sort
2024-05-02 08:13:15 +08:00

58 lines
1.2 KiB
TypeScript

import type { PublicSuffixList } from '@gorhill/publicsuffixlist';
import { createCachedGorhillGetDomain } from './cached-tld-parse';
const compare = (a: string | null, b: string | null) => {
if (a === b) return 0;
if (b == null) {
return 1;
}
if (a == null) {
return -1;
}
const aLen = a.length;
const r = aLen - b.length;
if (r > 0) {
return 1;
}
if (r < 0) {
return -1;
}
for (let i = 0; i < aLen; i++) {
if (b[i] == null) {
return 1;
}
if (a[i] < b[i]) {
return -1;
}
if (a[i] > b[i]) {
return 1;
}
}
return 0;
};
export const sortDomains = (inputs: string[], gorhill: PublicSuffixList) => {
const getDomain = createCachedGorhillGetDomain(gorhill);
const domains = inputs.reduce<Map<string, string>>((acc, cur) => {
if (!acc.has(cur)) acc.set(cur, getDomain(cur));
return acc;
}, new Map());
const sorter = (a: string, b: string) => {
if (a === b) return 0;
const $a = domains.get(a)!;
const $b = domains.get(b)!;
// avoid compare same thing twice
if (a === $a && b === $b) {
return compare(a, b);
}
return compare($a, $b) || compare(a, b);
};
return inputs.sort(sorter);
};