@@ -4,6 +4,8 @@ namespace ts {
44 // between browsers and NodeJS:
55
66 export interface PerformanceHooks {
7+ /** Indicates whether we should write native performance events */
8+ shouldWriteNativeEvents : boolean ;
79 performance : Performance ;
810 PerformanceObserver : PerformanceObserverConstructor ;
911 }
@@ -37,6 +39,7 @@ namespace ts {
3739 export type PerformanceEntryList = PerformanceEntry [ ] ;
3840
3941 // Browser globals for the Web Performance User Timings API
42+ declare const process : any ;
4043 declare const performance : Performance | undefined ;
4144 declare const PerformanceObserver : PerformanceObserverConstructor | undefined ;
4245
@@ -55,44 +58,49 @@ namespace ts {
5558 typeof PerformanceObserver === "function" &&
5659 hasRequiredAPI ( performance , PerformanceObserver ) ) {
5760 return {
61+ // For now we always write native performance events when running in the browser. We may
62+ // make this conditional in the future if we find that native web performance hooks
63+ // in the browser also slow down compilation.
64+ shouldWriteNativeEvents : true ,
5865 performance,
5966 PerformanceObserver
6067 } ;
6168 }
6269 }
6370
6471 function tryGetNodePerformanceHooks ( ) : PerformanceHooks | undefined {
65- if ( typeof module === "object" && typeof require === "function" ) {
72+ if ( typeof process !== "undefined" && process . nextTick && ! process . browser && typeof module === "object" && typeof require === "function" ) {
6673 try {
67- const { performance, PerformanceObserver } = require ( "perf_hooks" ) as typeof import ( "perf_hooks" ) ;
68- if ( hasRequiredAPI ( performance , PerformanceObserver ) ) {
74+ let performance : Performance ;
75+ const { performance : nodePerformance , PerformanceObserver } = require ( "perf_hooks" ) as typeof import ( "perf_hooks" ) ;
76+ if ( hasRequiredAPI ( nodePerformance , PerformanceObserver ) ) {
77+ performance = nodePerformance ;
6978 // There is a bug in Node's performance.measure prior to 12.16.3/13.13.0 that does not
7079 // match the Web Performance API specification. Node's implementation did not allow
7180 // optional `start` and `end` arguments for `performance.measure`.
7281 // See https://github.com/nodejs/node/pull/32651 for more information.
7382 const version = new Version ( process . versions . node ) ;
7483 const range = new VersionRange ( "<12.16.3 || 13 <13.13" ) ;
7584 if ( range . test ( version ) ) {
76- return {
77- performance : {
78- get timeOrigin ( ) { return performance . timeOrigin ; } ,
79- now ( ) { return performance . now ( ) ; } ,
80- mark ( name ) { return performance . mark ( name ) ; } ,
81- measure ( name , start = "nodeStart" , end ?) {
82- if ( end === undefined ) {
83- end = "__performance.measure-fix__" ;
84- performance . mark ( end ) ;
85- }
86- performance . measure ( name , start , end ) ;
87- if ( end === "__performance.measure-fix__" ) {
88- performance . clearMarks ( "__performance.measure-fix__" ) ;
89- }
85+ performance = {
86+ get timeOrigin ( ) { return nodePerformance . timeOrigin ; } ,
87+ now ( ) { return nodePerformance . now ( ) ; } ,
88+ mark ( name ) { return nodePerformance . mark ( name ) ; } ,
89+ measure ( name , start = "nodeStart" , end ?) {
90+ if ( end === undefined ) {
91+ end = "__performance.measure-fix__" ;
92+ nodePerformance . mark ( end ) ;
9093 }
91- } ,
92- PerformanceObserver
94+ nodePerformance . measure ( name , start , end ) ;
95+ if ( end === "__performance.measure-fix__" ) {
96+ nodePerformance . clearMarks ( "__performance.measure-fix__" ) ;
97+ }
98+ }
9399 } ;
94100 }
95101 return {
102+ // By default, only write native events when generating a cpu profile or using the v8 profiler.
103+ shouldWriteNativeEvents : false ,
96104 performance,
97105 PerformanceObserver
98106 } ;
0 commit comments