11/*@internal */
22/** Performance measurements for the compiler. */
33namespace 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-
74 let perfHooks : PerformanceHooks | undefined ;
85 let perfObserver : PerformanceObserver | undefined ;
96 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 ;
1110
1211 export interface Timer {
1312 enter ( ) : void ;
@@ -50,9 +49,8 @@ namespace ts.performance {
5049 * @param markName The name of the mark.
5150 */
5251 export function mark ( markName : string ) {
53- if ( perfHooks && enabled ) {
54- perfHooks . performance . mark ( markName ) ;
55- profilerEvent ( markName ) ;
52+ if ( performanceImpl ) {
53+ performanceImpl . mark ( markName ) ;
5654 }
5755 }
5856
@@ -66,17 +64,17 @@ namespace ts.performance {
6664 * used.
6765 */
6866 export function measure ( measureName : string , startMarkName ?: string , endMarkName ?: string ) {
69- if ( perfHooks && enabled ) {
67+ if ( performanceImpl ) {
7068 // NodeJS perf_hooks depends on call arity, not 'undefined' checks, so we
7169 // need to be sure we call 'measure' with the correct number of arguments.
7270 if ( startMarkName === undefined ) {
73- perfHooks . performance . measure ( measureName ) ;
71+ performanceImpl . measure ( measureName ) ;
7472 }
7573 else if ( endMarkName === undefined ) {
76- perfHooks . performance . measure ( measureName , startMarkName ) ;
74+ performanceImpl . measure ( measureName , startMarkName ) ;
7775 }
7876 else {
79- perfHooks . performance . measure ( measureName , startMarkName , endMarkName ) ;
77+ performanceImpl . measure ( measureName , startMarkName , endMarkName ) ;
8078 }
8179 }
8280 }
@@ -114,24 +112,24 @@ namespace ts.performance {
114112 * Indicates whether the performance API is enabled.
115113 */
116114 export function isEnabled ( ) {
117- return enabled ;
115+ return ! ! performanceImpl ;
118116 }
119117
120118 /** Enables (and resets) performance measurements for the compiler. */
121119 export function enable ( ) {
122- if ( ! enabled ) {
120+ if ( ! performanceImpl ) {
123121 perfHooks ||= tryGetNativePerformanceHooks ( ) || ShimPerformance ?. createPerformanceHooksShim ( timestamp ) ;
124122 if ( ! perfHooks ) return false ;
125123 perfObserver ||= new perfHooks . PerformanceObserver ( list => perfEntryList = list ) ;
126124 perfObserver . observe ( { entryTypes : [ "mark" , "measure" ] } ) ;
127- enabled = true ;
125+ performanceImpl = perfHooks . performance ;
128126 }
129127 return true ;
130128 }
131129
132130 /** Disables performance measurements for the compiler. */
133131 export function disable ( ) {
134132 perfObserver ?. disconnect ( ) ;
135- enabled = false ;
133+ performanceImpl = undefined ;
136134 }
137135}
0 commit comments