3
3
namespace ts . performance {
4
4
let perfHooks : PerformanceHooks | undefined ;
5
5
let perfObserver : PerformanceObserver | undefined ;
6
- let perfEntryList : PerformanceObserverEntryList | undefined ;
7
6
// when set, indicates the implementation of `Performance` to use for user timing.
8
7
// when unset, indicates user timing is unavailable or disabled.
9
8
let performanceImpl : Performance | undefined ;
@@ -42,6 +41,8 @@ namespace ts.performance {
42
41
}
43
42
44
43
export const nullTimer : Timer = { enter : noop , exit : noop } ;
44
+ const counts = new Map < string , number > ( ) ;
45
+ const durations = new Map < string , number > ( ) ;
45
46
46
47
/**
47
48
* Marks a performance event.
@@ -71,7 +72,7 @@ namespace ts.performance {
71
72
* @param markName The name of the mark.
72
73
*/
73
74
export function getCount ( markName : string ) {
74
- return perfEntryList ?. getEntriesByName ( markName , "mark" ) . length || 0 ;
75
+ return counts . get ( markName ) || 0 ;
75
76
}
76
77
77
78
/**
@@ -80,7 +81,7 @@ namespace ts.performance {
80
81
* @param measureName The name of the measure whose durations should be accumulated.
81
82
*/
82
83
export function getDuration ( measureName : string ) {
83
- return perfEntryList ?. getEntriesByName ( measureName , "measure" ) . reduce ( ( a , entry ) => a + entry . duration , 0 ) || 0 ;
84
+ return durations . get ( measureName ) || 0 ;
84
85
}
85
86
86
87
/**
@@ -89,7 +90,7 @@ namespace ts.performance {
89
90
* @param cb The action to perform for each measure
90
91
*/
91
92
export function forEachMeasure ( cb : ( measureName : string , duration : number ) => void ) {
92
- perfEntryList ?. getEntriesByType ( "measure" ) . forEach ( ( { name , duration } ) => { cb ( name , duration ) ; } ) ;
93
+ durations . forEach ( ( duration , measureName ) => cb ( measureName , duration ) ) ;
93
94
}
94
95
95
96
/**
@@ -104,7 +105,7 @@ namespace ts.performance {
104
105
if ( ! performanceImpl ) {
105
106
perfHooks ||= tryGetNativePerformanceHooks ( ) ;
106
107
if ( ! perfHooks ) return false ;
107
- perfObserver ||= new perfHooks . PerformanceObserver ( list => perfEntryList = list ) ;
108
+ perfObserver ||= new perfHooks . PerformanceObserver ( updateStatisticsFromList ) ;
108
109
perfObserver . observe ( { entryTypes : [ "mark" , "measure" ] } ) ;
109
110
performanceImpl = perfHooks . performance ;
110
111
}
@@ -115,5 +116,16 @@ namespace ts.performance {
115
116
export function disable ( ) {
116
117
perfObserver ?. disconnect ( ) ;
117
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
+ }
118
130
}
119
131
}
0 commit comments