@@ -7,104 +7,86 @@ import {
7
7
8
8
/** @internal */
9
9
export interface PerformanceHooks {
10
- /** Indicates whether we should write native performance events */
11
10
shouldWriteNativeEvents : boolean ;
12
- performance : Performance ;
13
- PerformanceObserver : PerformanceObserverConstructor ;
11
+ performance ? : Performance ;
12
+ performanceTime ?: PerformanceTime ;
14
13
}
15
14
16
15
/** @internal */
17
- export interface Performance {
18
- mark ( name : string ) : void ;
19
- measure ( name : string , startMark ?: string , endMark ?: string ) : void ;
20
- clearMeasures ( name ?: string ) : void ;
21
- clearMarks ( name ?: string ) : void ;
16
+ export interface PerformanceTime {
22
17
now ( ) : number ;
23
18
timeOrigin : number ;
24
19
}
25
20
26
21
/** @internal */
27
- export interface PerformanceEntry {
28
- name : string ;
29
- entryType : string ;
30
- startTime : number ;
31
- duration : number ;
32
- }
33
-
34
- /** @internal */
35
- export interface PerformanceObserverEntryList {
36
- getEntries ( ) : PerformanceEntryList ;
37
- getEntriesByName ( name : string , type ?: string ) : PerformanceEntryList ;
38
- getEntriesByType ( type : string ) : PerformanceEntryList ;
39
- }
40
-
41
- /** @internal */
42
- export interface PerformanceObserver {
43
- disconnect ( ) : void ;
44
- observe ( options : { entryTypes : readonly ( "mark" | "measure" ) [ ] ; } ) : void ;
22
+ export interface Performance extends PerformanceTime {
23
+ mark ( name : string ) : void ;
24
+ measure ( name : string , startMark ?: string , endMark ?: string ) : void ;
25
+ clearMeasures ( name ?: string ) : void ;
26
+ clearMarks ( name ?: string ) : void ;
45
27
}
46
28
47
- /** @internal */
48
- export type PerformanceObserverConstructor = new ( callback : ( list : PerformanceObserverEntryList , observer : PerformanceObserver ) => void ) => PerformanceObserver ;
49
- /** @internal */
50
- export type PerformanceEntryList = PerformanceEntry [ ] ;
51
-
52
29
// Browser globals for the Web Performance User Timings API
53
30
declare const performance : Performance | undefined ;
54
- declare const PerformanceObserver : PerformanceObserverConstructor | undefined ;
55
31
56
- // eslint-disable-next-line @typescript-eslint/naming-convention
57
- function hasRequiredAPI ( performance : Performance | undefined , PerformanceObserver : PerformanceObserverConstructor | undefined ) {
58
- return typeof performance === "object" &&
59
- typeof performance . timeOrigin === "number" &&
60
- typeof performance . mark === "function" &&
61
- typeof performance . measure === "function" &&
62
- typeof performance . now === "function" &&
63
- typeof performance . clearMarks === "function" &&
64
- typeof performance . clearMeasures === "function" &&
65
- typeof PerformanceObserver === "function" ;
66
- }
32
+ function tryGetPerformance ( ) {
33
+ if ( isNodeLikeSystem ( ) ) {
34
+ try {
35
+ // By default, only write native events when generating a cpu profile or using the v8 profiler.
36
+ const { performance } = require ( "perf_hooks" ) as typeof import ( "perf_hooks" ) ;
37
+ return {
38
+ shouldWriteNativeEvents : false ,
39
+ performance,
40
+ } ;
41
+ }
42
+ catch {
43
+ // ignore errors
44
+ }
45
+ }
67
46
68
- function tryGetWebPerformanceHooks ( ) : PerformanceHooks | undefined {
69
- if (
70
- typeof performance === "object" &&
71
- typeof PerformanceObserver === "function" &&
72
- hasRequiredAPI ( performance , PerformanceObserver )
73
- ) {
47
+ if ( typeof performance === "object" ) {
48
+ // For now we always write native performance events when running in the browser. We may
49
+ // make this conditional in the future if we find that native web performance hooks
50
+ // in the browser also slow down compilation.
74
51
return {
75
- // For now we always write native performance events when running in the browser. We may
76
- // make this conditional in the future if we find that native web performance hooks
77
- // in the browser also slow down compilation.
78
52
shouldWriteNativeEvents : true ,
79
53
performance,
80
- PerformanceObserver,
81
54
} ;
82
55
}
56
+
57
+ return undefined ;
83
58
}
84
59
85
- function tryGetNodePerformanceHooks ( ) : PerformanceHooks | undefined {
86
- if ( isNodeLikeSystem ( ) ) {
87
- try {
88
- const { performance, PerformanceObserver } = require ( "perf_hooks" ) as typeof import ( "perf_hooks" ) ;
89
- if ( hasRequiredAPI ( performance , PerformanceObserver ) ) {
90
- return {
91
- // By default, only write native events when generating a cpu profile or using the v8 profiler.
92
- shouldWriteNativeEvents : false ,
93
- performance,
94
- PerformanceObserver,
95
- } ;
96
- }
97
- }
98
- catch {
99
- // ignore errors
100
- }
60
+ function tryGetPerformanceHooks ( ) : PerformanceHooks | undefined {
61
+ const p = tryGetPerformance ( ) ;
62
+ if ( ! p ) return undefined ;
63
+ const { shouldWriteNativeEvents, performance } = p ;
64
+
65
+ const hooks : PerformanceHooks = {
66
+ shouldWriteNativeEvents,
67
+ performance : undefined ,
68
+ performanceTime : undefined ,
69
+ } ;
70
+
71
+ if ( typeof performance . timeOrigin === "number" && typeof performance . now === "function" ) {
72
+ hooks . performanceTime = performance ;
101
73
}
74
+
75
+ if (
76
+ hooks . performanceTime &&
77
+ typeof performance . mark === "function" &&
78
+ typeof performance . measure === "function" &&
79
+ typeof performance . clearMarks === "function" &&
80
+ typeof performance . clearMeasures === "function"
81
+ ) {
82
+ hooks . performance = performance ;
83
+ }
84
+
85
+ return hooks ;
102
86
}
103
87
104
- // Unlike with the native Map/Set 'tryGet' functions in corePublic.ts, we eagerly evaluate these
105
- // since we will need them for `timestamp`, below.
106
- const nativePerformanceHooks = tryGetWebPerformanceHooks ( ) || tryGetNodePerformanceHooks ( ) ;
107
- const nativePerformance = nativePerformanceHooks ?. performance ;
88
+ const nativePerformanceHooks = tryGetPerformanceHooks ( ) ;
89
+ const nativePerformanceTime = nativePerformanceHooks ?. performanceTime ;
108
90
109
91
/** @internal */
110
92
export function tryGetNativePerformanceHooks ( ) {
@@ -116,6 +98,4 @@ export function tryGetNativePerformanceHooks() {
116
98
*
117
99
* @internal
118
100
*/
119
- export const timestamp = nativePerformance ? ( ) => nativePerformance . now ( ) :
120
- Date . now ? Date . now :
121
- ( ) => + ( new Date ( ) ) ;
101
+ export const timestamp = nativePerformanceTime ? ( ) => nativePerformanceTime . now ( ) : Date . now ;
0 commit comments