33namespace ts . performance {
44 let perfHooks : PerformanceHooks | undefined ;
55 let perfObserver : PerformanceObserver | undefined ;
6- let perfEntryList : PerformanceObserverEntryList | undefined ;
76 // when set, indicates the implementation of `Performance` to use for user timing.
87 // when unset, indicates user timing is unavailable or disabled.
98 let performanceImpl : Performance | undefined ;
@@ -42,6 +41,8 @@ namespace ts.performance {
4241 }
4342
4443 export const nullTimer : Timer = { enter : noop , exit : noop } ;
44+ const counts = new Map < string , number > ( ) ;
45+ const durations = new Map < string , number > ( ) ;
4546
4647 /**
4748 * Marks a performance event.
@@ -71,7 +72,7 @@ namespace ts.performance {
7172 * @param markName The name of the mark.
7273 */
7374 export function getCount ( markName : string ) {
74- return perfEntryList ?. getEntriesByName ( markName , "mark" ) . length || 0 ;
75+ return counts . get ( markName ) || 0 ;
7576 }
7677
7778 /**
@@ -80,7 +81,7 @@ namespace ts.performance {
8081 * @param measureName The name of the measure whose durations should be accumulated.
8182 */
8283 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 ;
8485 }
8586
8687 /**
@@ -89,7 +90,7 @@ namespace ts.performance {
8990 * @param cb The action to perform for each measure
9091 */
9192 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 ) ) ;
9394 }
9495
9596 /**
@@ -104,7 +105,7 @@ namespace ts.performance {
104105 if ( ! performanceImpl ) {
105106 perfHooks ||= tryGetNativePerformanceHooks ( ) ;
106107 if ( ! perfHooks ) return false ;
107- perfObserver ||= new perfHooks . PerformanceObserver ( list => perfEntryList = list ) ;
108+ perfObserver ||= new perfHooks . PerformanceObserver ( updateStatisticsFromList ) ;
108109 perfObserver . observe ( { entryTypes : [ "mark" , "measure" ] } ) ;
109110 performanceImpl = perfHooks . performance ;
110111 }
@@ -115,5 +116,16 @@ namespace ts.performance {
115116 export function disable ( ) {
116117 perfObserver ?. disconnect ( ) ;
117118 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+ }
118130 }
119131}
0 commit comments