mirror of
https://github.com/SukkaW/Surge.git
synced 2025-12-12 17:20:35 +08:00
Perf: make sort domain faster
This commit is contained in:
parent
22d738d99d
commit
1b116637d2
@ -1,5 +1,4 @@
|
|||||||
import { fetchRemoteTextByLine } from './fetch-text-by-line';
|
import { fetchRemoteTextByLine } from './fetch-text-by-line';
|
||||||
import { getGorhillPublicSuffixPromise } from './get-gorhill-publicsuffix';
|
|
||||||
import { processLineFromReadline } from './process-line';
|
import { processLineFromReadline } from './process-line';
|
||||||
import { sortDomains } from './stable-sort-domain';
|
import { sortDomains } from './stable-sort-domain';
|
||||||
|
|
||||||
|
|||||||
@ -1,21 +1,7 @@
|
|||||||
// eslint-disable-next-line import-x/no-unresolved -- bun
|
// eslint-disable-next-line import-x/no-unresolved -- bun
|
||||||
import { describe, expect, it } from 'bun:test';
|
import { describe, expect, it } from 'bun:test';
|
||||||
|
|
||||||
import { compare, sortDomains } from './stable-sort-domain';
|
import { 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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('sortDomains', () => {
|
describe('sortDomains', () => {
|
||||||
it('basic', () => {
|
it('basic', () => {
|
||||||
|
|||||||
@ -6,36 +6,20 @@ export const compare = (a: string, b: string) => {
|
|||||||
|
|
||||||
const aLen = a.length;
|
const aLen = a.length;
|
||||||
const r = aLen - b.length;
|
const r = aLen - b.length;
|
||||||
if (r > 0) {
|
if (r !== 0) return r;
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
if (r < 0) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = 0; i < aLen; i++) {
|
return a.localeCompare(b);
|
||||||
// if (b[i] == null) {
|
|
||||||
// return 1;
|
|
||||||
// }
|
|
||||||
if (a[i] < b[i]) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
if (a[i] > b[i]) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const tldtsOpt = { allowPrivateDomains: false, detectIp: false, validateHostname: false };
|
const tldtsOpt = { allowPrivateDomains: false, detectIp: false, validateHostname: false };
|
||||||
|
|
||||||
export const sortDomains = (inputs: string[]) => {
|
export const sortDomains = (inputs: string[]) => {
|
||||||
const domains = inputs.reduce<Map<string, string>>((acc, cur) => {
|
const domains = inputs.reduce<Map<string, string>>((domains, cur) => {
|
||||||
if (!acc.has(cur)) {
|
if (!domains.has(cur)) {
|
||||||
const topD = tldts.getDomain(cur, tldtsOpt);
|
const topD = tldts.getDomain(cur, tldtsOpt);
|
||||||
acc.set(cur, topD ?? cur);
|
domains.set(cur, topD ?? cur);
|
||||||
};
|
};
|
||||||
return acc;
|
return domains;
|
||||||
}, new Map());
|
}, new Map());
|
||||||
|
|
||||||
const sorter = (a: string, b: string) => {
|
const sorter = (a: string, b: string) => {
|
||||||
@ -47,7 +31,7 @@ export const sortDomains = (inputs: string[]) => {
|
|||||||
if (a === $a && b === $b) {
|
if (a === $a && b === $b) {
|
||||||
return compare(a, b);
|
return compare(a, b);
|
||||||
}
|
}
|
||||||
return compare($a, $b) || compare(a, b);
|
return $a.localeCompare($b) || compare(a, b);
|
||||||
};
|
};
|
||||||
|
|
||||||
sort(inputs, sorter);
|
sort(inputs, sorter);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user