Perf: use Bun.peek() to save a few ticks

This commit is contained in:
SukkaW
2024-01-28 22:29:14 +08:00
parent c95e96fc61
commit e626a6b5d2
13 changed files with 108 additions and 47 deletions

View File

@@ -5,11 +5,11 @@ import { fetchRemoteTextByLine, readFileIntoProcessedArray } from './lib/fetch-t
import { task } from './trace';
import { SHARED_DESCRIPTION } from './lib/constants';
import { isProbablyIpv4, isProbablyIpv6 } from './lib/is-fast-ip';
import { TTL, deserializeArray, fsCache, serializeArray } from './lib/cache-filesystem';
import { TTL, deserializeArray, fsFetchCache, serializeArray } from './lib/cache-filesystem';
const URL = 'https://raw.githubusercontent.com/felixonmars/dnsmasq-china-list/master/bogus-nxdomain.china.conf';
const getBogusNxDomainIPsPromise = fsCache.apply(
const getBogusNxDomainIPsPromise = fsFetchCache.apply(
URL,
async () => {
const result: string[] = [];
@@ -34,7 +34,13 @@ const getBogusNxDomainIPsPromise = fsCache.apply(
export const buildAntiBogusDomain = task(import.meta.path, async (span) => {
const result: string[] = await readFileIntoProcessedArray(path.resolve(import.meta.dir, '../Source/ip/reject.conf'));
result.push(...(await getBogusNxDomainIPsPromise));
const peeked = Bun.peek(getBogusNxDomainIPsPromise);
const bogusNxDomainIPs = peeked === getBogusNxDomainIPsPromise
? await span.traceChild('get bogus nxdomain ips').traceAsyncFn(() => getBogusNxDomainIPsPromise)
: (peeked as string[]);
result.push(...bogusNxDomainIPs);
const description = [
...SHARED_DESCRIPTION,