Chore: use Bun.nanoseconds() over performance.now()

This commit is contained in:
SukkaW 2023-12-03 04:00:56 +08:00
parent d4ff4c5b2d
commit 17d69a975a
5 changed files with 20 additions and 23 deletions

View File

@ -53,7 +53,7 @@ export const buildInternalCDNDomains = task(import.meta.path, async () => {
addApexDomain(line.replace('DOMAIN,', '')); addApexDomain(line.replace('DOMAIN,', ''));
} else if (line.startsWith('DOMAIN-KEYWORD')) { } else if (line.startsWith('DOMAIN-KEYWORD')) {
keywords.add(escapeRegExp(line.replace('DOMAIN-KEYWORD,', ''))); keywords.add(escapeRegExp(line.replace('DOMAIN-KEYWORD,', '')));
} else if (line.startsWith('USER-AGENT,') || line.startsWith('PROCESS-NAME,')) { } else if (line.startsWith('USER-AGENT,') || line.startsWith('PROCESS-NAME,') || line.startsWith('URL-REGEX,')) {
// do nothing // do nothing
} else if (processLine(line)) { } else if (processLine(line)) {
console.warn('[drop line from ruleset]', line); console.warn('[drop line from ruleset]', line);

View File

@ -21,7 +21,7 @@ export async function compareAndWriteFile(linesA: string[], filePath: string) {
const lineA = linesA[index]; const lineA = linesA[index];
index++; index++;
if (lineA === undefined) { if (typeof lineA !== 'string') {
// The file becomes smaller // The file becomes smaller
isEqual = false; isEqual = false;
break; break;

View File

@ -4,7 +4,6 @@ import * as tldts from './cached-tld-parse';
import { fetchRemoteTextAndCreateReadlineInterface } from './fetch-remote-text-by-line'; import { fetchRemoteTextAndCreateReadlineInterface } from './fetch-remote-text-by-line';
import { NetworkFilter } from '@cliqz/adblocker'; import { NetworkFilter } from '@cliqz/adblocker';
import { processLine } from './process-line'; import { processLine } from './process-line';
import { performance } from 'perf_hooks';
import { getGorhillPublicSuffixPromise } from './get-gorhill-publicsuffix'; import { getGorhillPublicSuffixPromise } from './get-gorhill-publicsuffix';
import type { PublicSuffixList } from 'gorhill-publicsuffixlist'; import type { PublicSuffixList } from 'gorhill-publicsuffixlist';
@ -104,7 +103,7 @@ export async function processFilterRules(
filterRulesUrl: string | URL, filterRulesUrl: string | URL,
fallbackUrls?: ReadonlyArray<string | URL> | undefined fallbackUrls?: ReadonlyArray<string | URL> | undefined
): Promise<{ white: Set<string>, black: Set<string>, foundDebugDomain: boolean }> { ): Promise<{ white: Set<string>, black: Set<string>, foundDebugDomain: boolean }> {
const runStart = performance.now(); const runStart = Bun.nanoseconds();
const whitelistDomainSets = new Set<string>(); const whitelistDomainSets = new Set<string>();
const blacklistDomainSets = new Set<string>(); const blacklistDomainSets = new Set<string>();
@ -174,10 +173,10 @@ export async function processFilterRules(
if (!fallbackUrls || fallbackUrls.length === 0) { if (!fallbackUrls || fallbackUrls.length === 0) {
downloadTime = 0; downloadTime = 0;
let last = performance.now(); let last = Bun.nanoseconds();
for await (const line of await fetchRemoteTextAndCreateReadlineInterface(filterRulesUrl)) { for await (const line of await fetchRemoteTextAndCreateReadlineInterface(filterRulesUrl)) {
const now = performance.now(); const now = Bun.nanoseconds();
downloadTime += performance.now() - last; downloadTime += Bun.nanoseconds() - last;
last = now; last = now;
// don't trim here // don't trim here
lineCb(line); lineCb(line);
@ -185,7 +184,7 @@ export async function processFilterRules(
} else { } else {
let filterRules; let filterRules;
const downloadStart = performance.now(); const downloadStart = Bun.nanoseconds();
try { try {
const controller = new AbortController(); const controller = new AbortController();
@ -205,15 +204,15 @@ export async function processFilterRules(
console.log(`Download Rule for [${filterRulesUrl.toString()}] failed`); console.log(`Download Rule for [${filterRulesUrl.toString()}] failed`);
throw e; throw e;
} }
downloadTime = performance.now() - downloadStart; downloadTime = Bun.nanoseconds() - downloadStart;
for (let i = 0, len = filterRules.length; i < len; i++) { for (let i = 0, len = filterRules.length; i < len; i++) {
lineCb(filterRules[i]); lineCb(filterRules[i]);
} }
} }
console.log(` ┬ processFilterRules (${filterRulesUrl.toString()}): ${(performance.now() - runStart).toFixed(3)}ms`); console.log(` ┬ processFilterRules (${filterRulesUrl.toString()}): ${((Bun.nanoseconds() - runStart) / 1e6).toFixed(3)}ms`);
console.log(` └── download time: ${downloadTime.toFixed(3)}ms`); console.log(` └── download time: ${(downloadTime / 1e6).toFixed(3)}ms`);
return { return {
white: whitelistDomainSets, white: whitelistDomainSets,

View File

@ -28,9 +28,7 @@ export class PolyfillTextDecoderStream extends TransformStream<Uint8Array, strin
super({ super({
transform(chunk: Uint8Array, controller: TransformStreamDefaultController<string>) { transform(chunk: Uint8Array, controller: TransformStreamDefaultController<string>) {
const decoded = decoder.decode(chunk); const decoded = decoder.decode(chunk);
if (decoded.length > 0) { controller.enqueue(decoded);
controller.enqueue(decoded);
}
}, },
flush(controller: TransformStreamDefaultController<string>) { flush(controller: TransformStreamDefaultController<string>) {
// If {fatal: false} is in options (the default), then the final call to // If {fatal: false} is in options (the default), then the final call to

View File

@ -1,19 +1,19 @@
import path from 'path'; import path from 'path';
const traceSync = <T>(prefix: string, fn: () => T): T => { const traceSync = <T>(prefix: string, fn: () => T): T => {
const start = performance.now(); const start = Bun.nanoseconds();
const result = fn(); const result = fn();
const end = performance.now(); const end = Bun.nanoseconds();
console.log(`${prefix}: ${(end - start).toFixed(3)}ms`); console.log(`${prefix}: ${((end - start) / 1e6).toFixed(3)}ms`);
return result; return result;
}; };
export { traceSync }; export { traceSync };
const traceAsync = async <T>(prefix: string, fn: () => Promise<T>): Promise<T> => { const traceAsync = async <T>(prefix: string, fn: () => Promise<T>): Promise<T> => {
const start = performance.now(); const start = Bun.nanoseconds();
const result = await fn(); const result = await fn();
const end = performance.now(); const end = Bun.nanoseconds();
console.log(`${prefix}: ${(end - start).toFixed(3)}ms`); console.log(`${prefix}: ${((end - start) / 1e6).toFixed(3)}ms`);
return result; return result;
}; };
export { traceAsync }; export { traceAsync };
@ -28,10 +28,10 @@ const task = <T>(importMetaPath: string, fn: () => Promise<T>, customname: strin
const taskName = customname ?? path.basename(importMetaPath, path.extname(importMetaPath)); const taskName = customname ?? path.basename(importMetaPath, path.extname(importMetaPath));
return async () => { return async () => {
console.log(`🏃 [${taskName}] Start executing`); console.log(`🏃 [${taskName}] Start executing`);
const start = performance.now(); const start = Bun.nanoseconds();
await fn(); await fn();
const end = performance.now(); const end = Bun.nanoseconds();
console.log(`✅ [${taskName}] Executed successfully: ${(end - start).toFixed(3)}ms`); console.log(`✅ [${taskName}] Executed successfully: ${((end - start) / 1e6).toFixed(3)}ms`);
return { start, end, taskName } as TaskResult; return { start, end, taskName } as TaskResult;
}; };