cal.pub0.org/packages/lib/server/perfObserver.ts

26 lines
728 B
TypeScript
Raw Normal View History

2022-06-10 18:38:46 +00:00
import { PerformanceObserver } from "perf_hooks";
declare global {
// eslint-disable-next-line no-var
var perfObserver: PerformanceObserver | undefined;
}
export const perfObserver =
globalThis.perfObserver ||
2022-06-14 15:39:50 +00:00
new PerformanceObserver((items) => {
2022-06-10 18:38:46 +00:00
items.getEntries().forEach((entry) => {
// Log entry duration in seconds with four decimal places.
if (!!process.env.NEXT_PUBLIC_DEBUG)
console.log(entry.name.replace("$1", `${(entry.duration / 1000.0).toFixed(4)}s`));
2022-06-10 18:38:46 +00:00
});
});
2022-06-14 15:39:50 +00:00
perfObserver.observe({ entryTypes: ["measure"] });
2022-06-10 18:38:46 +00:00
if (process.env.NODE_ENV !== "production") {
globalThis.perfObserver = perfObserver;
}
export default perfObserver;
export { performance } from "perf_hooks";