Perf: use tldts-experimental when possible

This commit is contained in:
SukkaW
2024-05-26 04:32:08 +08:00
parent db6786a0d6
commit 1288460c48
9 changed files with 32 additions and 24 deletions

View File

@@ -1,4 +1,7 @@
import * as tldts from 'tldts';
// tldts-experimental is way faster than tldts, but very little bit inaccurate
// (since it is hashes based). But the result is still deterministic, which is
// enough when sorting.
import * as tldts from 'tldts-experimental';
import { sort } from './timsort';
export const compare = (a: string, b: string) => {
@@ -6,11 +9,11 @@ export const compare = (a: string, b: string) => {
return (a.length - b.length) || a.localeCompare(b);
};
const tldtsOpt = {
extractHostname: false,
const tldtsOpt: Parameters<typeof tldts.getDomain>[1] = {
allowPrivateDomains: false,
detectIp: false,
extractHostname: false,
validateHostname: false,
detectIp: false,
mixedInputs: false
};
@@ -36,14 +39,16 @@ export const sortDomains = (inputs: string[]) => {
const main_domain_a = domainMap.get(a)!;
const main_domain_b = domainMap.get(b)!;
let t = compare(main_domain_a, main_domain_b);
let t = compare(
main_domain_a,
main_domain_b
) || compare(
/** subdomain_a */ subdomainMap.get(a)!,
/** subdomain_b */ subdomainMap.get(b)!
);
if (t !== 0) return t;
if (t === 0) {
const subdomain_a = subdomainMap.get(a)!;
const subdomain_b = subdomainMap.get(b)!;
t = compare(subdomain_a, subdomain_b);
}
if (t === 0 && (a !== main_domain_a || b !== main_domain_b)) {
if (a !== main_domain_a || b !== main_domain_b) {
t = compare(a, b);
}