1
1
/*@internal */
2
2
/** Performance measurements for the compiler. */
3
3
namespace ts . performance {
4
- declare const onProfilerEvent : { ( markName : string ) : void ; profiler : boolean ; } ;
5
- const profilerEvent : ( markName : string ) => void = typeof onProfilerEvent === "function" && onProfilerEvent . profiler === true ? onProfilerEvent : noop ;
6
-
7
4
let perfHooks : PerformanceHooks | undefined ;
8
5
let perfObserver : PerformanceObserver | undefined ;
9
6
let perfEntryList : PerformanceObserverEntryList | undefined ;
10
- let enabled = false ;
7
+ // when set, indicates the implementation of `Performance` to use for user timing.
8
+ // when unset, indicates user timing is unavailable or disabled.
9
+ let performanceImpl : Performance | undefined ;
11
10
12
11
export interface Timer {
13
12
enter ( ) : void ;
@@ -50,9 +49,8 @@ namespace ts.performance {
50
49
* @param markName The name of the mark.
51
50
*/
52
51
export function mark ( markName : string ) {
53
- if ( perfHooks && enabled ) {
54
- perfHooks . performance . mark ( markName ) ;
55
- profilerEvent ( markName ) ;
52
+ if ( performanceImpl ) {
53
+ performanceImpl . mark ( markName ) ;
56
54
}
57
55
}
58
56
@@ -66,17 +64,17 @@ namespace ts.performance {
66
64
* used.
67
65
*/
68
66
export function measure ( measureName : string , startMarkName ?: string , endMarkName ?: string ) {
69
- if ( perfHooks && enabled ) {
67
+ if ( performanceImpl ) {
70
68
// NodeJS perf_hooks depends on call arity, not 'undefined' checks, so we
71
69
// need to be sure we call 'measure' with the correct number of arguments.
72
70
if ( startMarkName === undefined ) {
73
- perfHooks . performance . measure ( measureName ) ;
71
+ performanceImpl . measure ( measureName ) ;
74
72
}
75
73
else if ( endMarkName === undefined ) {
76
- perfHooks . performance . measure ( measureName , startMarkName ) ;
74
+ performanceImpl . measure ( measureName , startMarkName ) ;
77
75
}
78
76
else {
79
- perfHooks . performance . measure ( measureName , startMarkName , endMarkName ) ;
77
+ performanceImpl . measure ( measureName , startMarkName , endMarkName ) ;
80
78
}
81
79
}
82
80
}
@@ -114,24 +112,24 @@ namespace ts.performance {
114
112
* Indicates whether the performance API is enabled.
115
113
*/
116
114
export function isEnabled ( ) {
117
- return enabled ;
115
+ return ! ! performanceImpl ;
118
116
}
119
117
120
118
/** Enables (and resets) performance measurements for the compiler. */
121
119
export function enable ( ) {
122
- if ( ! enabled ) {
120
+ if ( ! performanceImpl ) {
123
121
perfHooks ||= tryGetNativePerformanceHooks ( ) || ShimPerformance ?. createPerformanceHooksShim ( timestamp ) ;
124
122
if ( ! perfHooks ) return false ;
125
123
perfObserver ||= new perfHooks . PerformanceObserver ( list => perfEntryList = list ) ;
126
124
perfObserver . observe ( { entryTypes : [ "mark" , "measure" ] } ) ;
127
- enabled = true ;
125
+ performanceImpl = perfHooks . performance ;
128
126
}
129
127
return true ;
130
128
}
131
129
132
130
/** Disables performance measurements for the compiler. */
133
131
export function disable ( ) {
134
132
perfObserver ?. disconnect ( ) ;
135
- enabled = false ;
133
+ performanceImpl = undefined ;
136
134
}
137
135
}
0 commit comments