22/** Performance measurements for the compiler. */
33namespace ts . performance {
44 declare const onProfilerEvent : { ( markName : string ) : void ; profiler : boolean ; } ;
5+ const profilerEvent : ( markName : string ) => void = typeof onProfilerEvent === "function" && onProfilerEvent . profiler === true ? onProfilerEvent : noop ;
56
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-
7+ let perfHooks : PerformanceHooks | undefined ;
8+ let perfObserver : PerformanceObserver | undefined ;
9+ let perfEntryList : PerformanceObserverEntryList | undefined ;
910 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 > ;
1411
1512 export interface Timer {
1613 enter ( ) : void ;
@@ -53,9 +50,8 @@ namespace ts.performance {
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 ) ;
53+ if ( perfHooks && enabled ) {
54+ perfHooks . performance . mark ( markName ) ;
5955 profilerEvent ( markName ) ;
6056 }
6157 }
@@ -70,10 +66,8 @@ namespace ts.performance {
7066 * used.
7167 */
7268 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 ) ) ;
69+ if ( perfHooks && enabled ) {
70+ perfHooks . performance . measure ( measureName , startMarkName , endMarkName ) ;
7771 }
7872 }
7973
@@ -83,7 +77,7 @@ namespace ts.performance {
8377 * @param markName The name of the mark.
8478 */
8579 export function getCount ( markName : string ) {
86- return counts && counts . get ( markName ) || 0 ;
80+ return perfEntryList ?. getEntriesByName ( markName , "mark" ) . length || 0 ;
8781 }
8882
8983 /**
@@ -92,7 +86,7 @@ namespace ts.performance {
9286 * @param measureName The name of the measure whose durations should be accumulated.
9387 */
9488 export function getDuration ( measureName : string ) {
95- return measures && measures . get ( measureName ) || 0 ;
89+ return perfEntryList ?. getEntriesByName ( measureName , "measure" ) . reduce ( ( a , entry ) => a + entry . duration , 0 ) || 0 ;
9690 }
9791
9892 /**
@@ -101,22 +95,25 @@ namespace ts.performance {
10195 * @param cb The action to perform for each measure
10296 */
10397 export function forEachMeasure ( cb : ( measureName : string , duration : number ) => void ) {
104- measures . forEach ( ( measure , key ) => {
105- cb ( key , measure ) ;
98+ perfEntryList ?. getEntriesByType ( " measure" ) . forEach ( entry => {
99+ cb ( entry . name , entry . duration ) ;
106100 } ) ;
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 ( ! enabled ) {
106+ perfHooks ||= tryGetNativePerformanceHooks ( ) || ShimPerformance ?. createPerformanceHooksShim ( timestamp ) ;
107+ if ( ! perfHooks ) throw new Error ( "TypeScript requires an environment that provides a compatible native Web Performance API implementation." ) ;
108+ perfObserver ||= new perfHooks . PerformanceObserver ( list => perfEntryList = list ) ;
109+ perfObserver . observe ( { entryTypes : [ "mark" , "measure" ] } ) ;
110+ enabled = true ;
111+ }
116112 }
117113
118114 /** Disables performance measurements for the compiler. */
119115 export function disable ( ) {
116+ perfObserver ?. disconnect ( ) ;
120117 enabled = false ;
121118 }
122119}
0 commit comments