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,', ''));
} else if (line.startsWith('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
} else if (processLine(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];
index++;
if (lineA === undefined) {
if (typeof lineA !== 'string') {
// The file becomes smaller
isEqual = false;
break;

View File

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

View File

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

View File

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