11/*@internal */
22/** Performance measurements for the compiler. */
33namespace ts . performance {
4- declare const onProfilerEvent : { ( markName : string ) : void ; profiler : boolean ; } ;
5-
6- // NOTE: cannot use ts.noop as core.ts loads after this
7- const profilerEvent : ( markName : string ) => void = typeof onProfilerEvent === "function" && onProfilerEvent . profiler === true ? onProfilerEvent : ( ) => { /*empty*/ } ;
8-
9- let enabled = false ;
10- let profilerStart = 0 ;
11- let counts : ESMap < string , number > ;
12- let marks : ESMap < string , number > ;
13- let measures : ESMap < string , number > ;
4+ let perfHooks : PerformanceHooks | undefined ;
5+ let perfObserver : PerformanceObserver | undefined ;
6+ // when set, indicates the implementation of `Performance` to use for user timing.
7+ // when unset, indicates user timing is unavailable or disabled.
8+ let performanceImpl : Performance | undefined ;
149
1510 export interface Timer {
1611 enter ( ) : void ;
@@ -46,18 +41,16 @@ namespace ts.performance {
4641 }
4742
4843 export const nullTimer : Timer = { enter : noop , exit : noop } ;
44+ const counts = new Map < string , number > ( ) ;
45+ const durations = new Map < string , number > ( ) ;
4946
5047 /**
5148 * Marks a performance event.
5249 *
5350 * @param markName The name of the mark.
5451 */
5552 export function mark ( markName : string ) {
56- if ( enabled ) {
57- marks . set ( markName , timestamp ( ) ) ;
58- counts . set ( markName , ( counts . get ( markName ) || 0 ) + 1 ) ;
59- profilerEvent ( markName ) ;
60- }
53+ performanceImpl ?. mark ( markName ) ;
6154 }
6255
6356 /**
@@ -70,11 +63,7 @@ namespace ts.performance {
7063 * used.
7164 */
7265 export function measure ( measureName : string , startMarkName ?: string , endMarkName ?: string ) {
73- if ( enabled ) {
74- const end = endMarkName && marks . get ( endMarkName ) || timestamp ( ) ;
75- const start = startMarkName && marks . get ( startMarkName ) || profilerStart ;
76- measures . set ( measureName , ( measures . get ( measureName ) || 0 ) + ( end - start ) ) ;
77- }
66+ performanceImpl ?. measure ( measureName , startMarkName , endMarkName ) ;
7867 }
7968
8069 /**
@@ -83,7 +72,7 @@ namespace ts.performance {
8372 * @param markName The name of the mark.
8473 */
8574 export function getCount ( markName : string ) {
86- return counts && counts . get ( markName ) || 0 ;
75+ return counts . get ( markName ) || 0 ;
8776 }
8877
8978 /**
@@ -92,7 +81,7 @@ namespace ts.performance {
9281 * @param measureName The name of the measure whose durations should be accumulated.
9382 */
9483 export function getDuration ( measureName : string ) {
95- return measures && measures . get ( measureName ) || 0 ;
84+ return durations . get ( measureName ) || 0 ;
9685 }
9786
9887 /**
@@ -101,22 +90,42 @@ namespace ts.performance {
10190 * @param cb The action to perform for each measure
10291 */
10392 export function forEachMeasure ( cb : ( measureName : string , duration : number ) => void ) {
104- measures . forEach ( ( measure , key ) => {
105- cb ( key , measure ) ;
106- } ) ;
93+ durations . forEach ( ( duration , measureName ) => cb ( measureName , duration ) ) ;
94+ }
95+
96+ /**
97+ * Indicates whether the performance API is enabled.
98+ */
99+ export function isEnabled ( ) {
100+ return ! ! performanceImpl ;
107101 }
108102
109103 /** Enables (and resets) performance measurements for the compiler. */
110104 export function enable ( ) {
111- counts = new Map < string , number > ( ) ;
112- marks = new Map < string , number > ( ) ;
113- measures = new Map < string , number > ( ) ;
114- enabled = true ;
115- profilerStart = timestamp ( ) ;
105+ if ( ! performanceImpl ) {
106+ perfHooks ||= tryGetNativePerformanceHooks ( ) ;
107+ if ( ! perfHooks ) return false ;
108+ perfObserver ||= new perfHooks . PerformanceObserver ( updateStatisticsFromList ) ;
109+ perfObserver . observe ( { entryTypes : [ "mark" , "measure" ] } ) ;
110+ performanceImpl = perfHooks . performance ;
111+ }
112+ return true ;
116113 }
117114
118115 /** Disables performance measurements for the compiler. */
119116 export function disable ( ) {
120- enabled = false ;
117+ perfObserver ?. disconnect ( ) ;
118+ performanceImpl = undefined ;
119+ counts . clear ( ) ;
120+ durations . clear ( ) ;
121+ }
122+
123+ function updateStatisticsFromList ( list : PerformanceObserverEntryList ) {
124+ for ( const mark of list . getEntriesByType ( "mark" ) ) {
125+ counts . set ( mark . name , ( counts . get ( mark . name ) || 0 ) + 1 ) ;
126+ }
127+ for ( const measure of list . getEntriesByType ( "measure" ) ) {
128+ durations . set ( measure . name , ( durations . get ( measure . name ) || 0 ) + measure . duration ) ;
129+ }
121130 }
122131}
0 commit comments