From 1b116637d28d95037b0a94a9998ed08842d5b518 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Sat, 11 May 2024 10:39:24 +0800 Subject: [PATCH] Perf: make sort domain faster --- Build/lib/stable-sort-domain.bench.ts | 1 - Build/lib/stable-sort-domain.test.ts | 16 +------------- Build/lib/stable-sort-domain.ts | 30 +++++++-------------------- 3 files changed, 8 insertions(+), 39 deletions(-) diff --git a/Build/lib/stable-sort-domain.bench.ts b/Build/lib/stable-sort-domain.bench.ts index 2fac4431..84636f68 100644 --- a/Build/lib/stable-sort-domain.bench.ts +++ b/Build/lib/stable-sort-domain.bench.ts @@ -1,5 +1,4 @@ import { fetchRemoteTextByLine } from './fetch-text-by-line'; -import { getGorhillPublicSuffixPromise } from './get-gorhill-publicsuffix'; import { processLineFromReadline } from './process-line'; import { sortDomains } from './stable-sort-domain'; diff --git a/Build/lib/stable-sort-domain.test.ts b/Build/lib/stable-sort-domain.test.ts index e9bab0b3..f517cf15 100644 --- a/Build/lib/stable-sort-domain.test.ts +++ b/Build/lib/stable-sort-domain.test.ts @@ -1,21 +1,7 @@ // eslint-disable-next-line import-x/no-unresolved -- bun import { describe, expect, it } from 'bun:test'; -import { compare, sortDomains } from './stable-sort-domain'; - -describe('compare', () => { - it('basic', () => { - expect( - compare('.s3-website.ap-northeast-3.amazonaws.com', '.s3.dualstack.ap-south-1.amazonaws.com') - ).toBe(1); - }); - - it('basic', () => { - expect( - compare('.s3-website.ap-northeast-3.amazonaws.com', '.s3.dualstack.ap-south-1.amazonaws.com') - ).toBe(1); - }); -}); +import { sortDomains } from './stable-sort-domain'; describe('sortDomains', () => { it('basic', () => { diff --git a/Build/lib/stable-sort-domain.ts b/Build/lib/stable-sort-domain.ts index 24d53bb1..b5ce578e 100644 --- a/Build/lib/stable-sort-domain.ts +++ b/Build/lib/stable-sort-domain.ts @@ -6,36 +6,20 @@ export const compare = (a: string, b: string) => { const aLen = a.length; const r = aLen - b.length; - if (r > 0) { - return 1; - } - if (r < 0) { - return -1; - } + if (r !== 0) return r; - 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; + return a.localeCompare(b); }; const tldtsOpt = { allowPrivateDomains: false, detectIp: false, validateHostname: false }; export const sortDomains = (inputs: string[]) => { - const domains = inputs.reduce>((acc, cur) => { - if (!acc.has(cur)) { + const domains = inputs.reduce>((domains, cur) => { + if (!domains.has(cur)) { const topD = tldts.getDomain(cur, tldtsOpt); - acc.set(cur, topD ?? cur); + domains.set(cur, topD ?? cur); }; - return acc; + return domains; }, new Map()); const sorter = (a: string, b: string) => { @@ -47,7 +31,7 @@ export const sortDomains = (inputs: string[]) => { if (a === $a && b === $b) { return compare(a, b); } - return compare($a, $b) || compare(a, b); + return $a.localeCompare($b) || compare(a, b); }; sort(inputs, sorter);