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

@@ -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;
};