-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathperformanceCore.ts
102 lines (86 loc) · 3.18 KB
/
performanceCore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import { isNodeLikeSystem } from "./_namespaces/ts.js";
// The following definitions provide the minimum compatible support for the Web Performance User Timings API
// between browsers and NodeJS:
/** @internal */
export interface PerformanceHooks {
shouldWriteNativeEvents: boolean;
performance?: Performance;
performanceTime?: PerformanceTime;
}
/** @internal */
export interface PerformanceTime {
now(): number;
timeOrigin: number;
}
/** @internal */
export interface Performance extends PerformanceTime {
mark(name: string): void;
measure(name: string, startMark?: string, endMark?: string): void;
clearMeasures(name?: string): void;
clearMarks(name?: string): void;
}
// Browser globals for the Web Performance User Timings API
declare const performance: Performance | undefined;
function tryGetPerformance() {
if (isNodeLikeSystem()) {
try {
// By default, only write native events when generating a cpu profile or using the v8 profiler.
// Some environments may polyfill this module with an empty object; verify the object has the expected shape.
const { performance } = require("perf_hooks") as Partial<typeof import("perf_hooks")>;
if (performance) {
return {
shouldWriteNativeEvents: false,
performance,
};
}
}
catch {
// ignore errors
}
}
if (typeof performance === "object") {
// For now we always write native performance events when running in the browser. We may
// make this conditional in the future if we find that native web performance hooks
// in the browser also slow down compilation.
return {
shouldWriteNativeEvents: true,
performance,
};
}
return undefined;
}
function tryGetPerformanceHooks(): PerformanceHooks | undefined {
const p = tryGetPerformance();
if (!p) return undefined;
const { shouldWriteNativeEvents, performance } = p;
const hooks: PerformanceHooks = {
shouldWriteNativeEvents,
performance: undefined,
performanceTime: undefined,
};
if (typeof performance.timeOrigin === "number" && typeof performance.now === "function") {
hooks.performanceTime = performance;
}
if (
hooks.performanceTime &&
typeof performance.mark === "function" &&
typeof performance.measure === "function" &&
typeof performance.clearMarks === "function" &&
typeof performance.clearMeasures === "function"
) {
hooks.performance = performance;
}
return hooks;
}
const nativePerformanceHooks = tryGetPerformanceHooks();
const nativePerformanceTime = nativePerformanceHooks?.performanceTime;
/** @internal */
export function tryGetNativePerformanceHooks(): PerformanceHooks | undefined {
return nativePerformanceHooks;
}
/**
* Gets a timestamp with (at least) ms resolution
*
* @internal
*/
export const timestamp: () => number = nativePerformanceTime ? () => nativePerformanceTime.now() : Date.now;