2
2
/** Performance measurements for the compiler. */
3
3
namespace ts . performance {
4
4
declare const onProfilerEvent : { ( markName : string ) : void ; profiler : boolean ; } ;
5
+ const profilerEvent : ( markName : string ) => void = typeof onProfilerEvent === "function" && onProfilerEvent . profiler === true ? onProfilerEvent : noop ;
5
6
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 ;
9
10
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 > ;
14
11
15
12
export interface Timer {
16
13
enter ( ) : void ;
@@ -53,9 +50,8 @@ namespace ts.performance {
53
50
* @param markName The name of the mark.
54
51
*/
55
52
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 ) ;
59
55
profilerEvent ( markName ) ;
60
56
}
61
57
}
@@ -70,10 +66,8 @@ namespace ts.performance {
70
66
* used.
71
67
*/
72
68
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 ) ;
77
71
}
78
72
}
79
73
@@ -83,7 +77,7 @@ namespace ts.performance {
83
77
* @param markName The name of the mark.
84
78
*/
85
79
export function getCount ( markName : string ) {
86
- return counts && counts . get ( markName ) || 0 ;
80
+ return perfEntryList ?. getEntriesByName ( markName , "mark" ) . length || 0 ;
87
81
}
88
82
89
83
/**
@@ -92,7 +86,7 @@ namespace ts.performance {
92
86
* @param measureName The name of the measure whose durations should be accumulated.
93
87
*/
94
88
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 ;
96
90
}
97
91
98
92
/**
@@ -101,22 +95,25 @@ namespace ts.performance {
101
95
* @param cb The action to perform for each measure
102
96
*/
103
97
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 ) ;
106
100
} ) ;
107
101
}
108
102
109
103
/** Enables (and resets) performance measurements for the compiler. */
110
104
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
+ }
116
112
}
117
113
118
114
/** Disables performance measurements for the compiler. */
119
115
export function disable ( ) {
116
+ perfObserver ?. disconnect ( ) ;
120
117
enabled = false ;
121
118
}
122
119
}
0 commit comments