11/*@internal */
22namespace ts {
3+ declare const performance : { now ?( ) : number } | undefined ;
4+ /** Gets a timestamp with (at least) ms resolution */
5+ export const timestamp = typeof performance !== "undefined" && performance . now ? performance . now : Date . now ? Date . now : ( ) => + ( new Date ( ) ) ;
6+ }
7+
8+ /*@internal */
9+ namespace ts . performance {
310 /** Performance measurements for the compiler. */
4- export namespace performance {
5- declare const onProfilerEvent : { ( markName : string ) : void ; profiler : boolean ; } ;
6- declare const performance : { now ?( ) : number } | undefined ;
7- let profilerEvent : ( markName : string ) => void ;
8- let markInternal : ( ) => number ;
9- let counters : Map < number > ;
10- let measures : Map < number > ;
11+ declare const onProfilerEvent : { ( markName : string ) : void ; profiler : boolean ; } ;
12+ let profilerEvent : ( markName : string ) => void ;
13+ let counters : Map < number > ;
14+ let measures : Map < number > ;
1115
12- /**
13- * Emit a performance event if ts-profiler is connected. This is primarily used
14- * to generate heap snapshots.
15- *
16- * @param eventName A name for the event.
17- */
18- export function emit ( eventName : string ) {
19- if ( profilerEvent ) {
20- profilerEvent ( eventName ) ;
21- }
16+ /**
17+ * Emit a performance event if ts-profiler is connected. This is primarily used
18+ * to generate heap snapshots.
19+ *
20+ * @param eventName A name for the event.
21+ */
22+ export function emit ( eventName : string ) {
23+ if ( profilerEvent ) {
24+ profilerEvent ( eventName ) ;
2225 }
26+ }
2327
24- /**
25- * Increments a counter with the specified name.
26- *
27- * @param counterName The name of the counter.
28- */
29- export function increment ( counterName : string ) {
30- if ( counters ) {
31- counters [ counterName ] = ( getProperty ( counters , counterName ) || 0 ) + 1 ;
32- }
28+ /**
29+ * Increments a counter with the specified name.
30+ *
31+ * @param counterName The name of the counter.
32+ */
33+ export function increment ( counterName : string ) {
34+ if ( counters ) {
35+ counters [ counterName ] = ( getProperty ( counters , counterName ) || 0 ) + 1 ;
3336 }
37+ }
3438
35- /**
36- * Gets the value of the counter with the specified name.
37- *
38- * @param counterName The name of the counter.
39- */
40- export function getCount ( counterName : string ) {
41- return counters && getProperty ( counters , counterName ) || 0 ;
42- }
39+ /**
40+ * Gets the value of the counter with the specified name.
41+ *
42+ * @param counterName The name of the counter.
43+ */
44+ export function getCount ( counterName : string ) {
45+ return counters && getProperty ( counters , counterName ) || 0 ;
46+ }
4347
44- /**
45- * Marks the start of a performance measurement.
46- */
47- export function mark ( ) {
48- return measures ? markInternal ( ) : 0 ;
49- }
48+ /**
49+ * Marks the start of a performance measurement.
50+ */
51+ export function mark ( ) {
52+ return measures ? timestamp ( ) : 0 ;
53+ }
5054
51- /**
52- * Adds a performance measurement with the specified name.
53- *
54- * @param measureName The name of the performance measurement.
55- * @param marker The timestamp of the starting mark.
56- */
57- export function measure ( measureName : string , marker : number ) {
58- if ( measures ) {
59- measures [ measureName ] = ( getProperty ( measures , measureName ) || 0 ) + ( Date . now ( ) - marker ) ;
60- }
55+ /**
56+ * Adds a performance measurement with the specified name.
57+ *
58+ * @param measureName The name of the performance measurement.
59+ * @param marker The timestamp of the starting mark.
60+ */
61+ export function measure ( measureName : string , marker : number ) {
62+ if ( measures ) {
63+ measures [ measureName ] = ( getProperty ( measures , measureName ) || 0 ) + ( timestamp ( ) - marker ) ;
6164 }
65+ }
6266
63- /**
64- * Iterate over each measure, performing some action
65- *
66- * @param cb The action to perform for each measure
67- */
68- export function forEachMeasure ( cb : ( measureName : string , duration : number ) => void ) {
69- return forEachKey ( measures , key => cb ( key , measures [ key ] ) ) ;
70- }
67+ /**
68+ * Iterate over each measure, performing some action
69+ *
70+ * @param cb The action to perform for each measure
71+ */
72+ export function forEachMeasure ( cb : ( measureName : string , duration : number ) => void ) {
73+ return forEachKey ( measures , key => cb ( key , measures [ key ] ) ) ;
74+ }
7175
72- /**
73- * Gets the total duration of all measurements with the supplied name.
74- *
75- * @param measureName The name of the measure whose durations should be accumulated.
76- */
77- export function getDuration ( measureName : string ) {
78- return measures && getProperty ( measures , measureName ) || 0 ;
79- }
76+ /**
77+ * Gets the total duration of all measurements with the supplied name.
78+ *
79+ * @param measureName The name of the measure whose durations should be accumulated.
80+ */
81+ export function getDuration ( measureName : string ) {
82+ return measures && getProperty ( measures , measureName ) || 0 ;
83+ }
8084
81- /** Enables (and resets) performance measurements for the compiler. */
82- export function enable ( ) {
83- counters = { } ;
84- measures = {
85- "I/O Read" : 0 ,
86- "I/O Write" : 0 ,
87- "Program" : 0 ,
88- "Parse" : 0 ,
89- "Bind" : 0 ,
90- "Check" : 0 ,
91- "Emit" : 0 ,
92- } ;
85+ /** Enables (and resets) performance measurements for the compiler. */
86+ export function enable ( ) {
87+ counters = { } ;
88+ measures = {
89+ "I/O Read" : 0 ,
90+ "I/O Write" : 0 ,
91+ "Program" : 0 ,
92+ "Parse" : 0 ,
93+ "Bind" : 0 ,
94+ "Check" : 0 ,
95+ "Emit" : 0 ,
96+ } ;
9397
94- profilerEvent = typeof onProfilerEvent === "function" && onProfilerEvent . profiler === true
95- ? onProfilerEvent
96- : undefined ;
97- markInternal = performance && performance . now ? performance . now : Date . now ? Date . now : ( ) => new Date ( ) . getTime ( ) ;
98- }
98+ profilerEvent = typeof onProfilerEvent === "function" && onProfilerEvent . profiler === true
99+ ? onProfilerEvent
100+ : undefined ;
101+ }
99102
100- /** Disables (and clears) performance measurements for the compiler. */
101- export function disable ( ) {
102- counters = undefined ;
103- measures = undefined ;
104- profilerEvent = undefined ;
105- }
103+ /** Disables (and clears) performance measurements for the compiler. */
104+ export function disable ( ) {
105+ counters = undefined ;
106+ measures = undefined ;
107+ profilerEvent = undefined ;
106108 }
107109}
0 commit comments