@@ -9,7 +9,6 @@ namespace ts {
99 }
1010
1111 export interface Performance {
12- clearMarks ( name ?: string ) : void ;
1312 mark ( name : string ) : void ;
1413 measure ( name : string , startMark ?: string , endMark ?: string ) : void ;
1514 now ( ) : number ;
@@ -41,14 +40,19 @@ namespace ts {
4140 declare const performance : Performance | undefined ;
4241 declare const PerformanceObserver : PerformanceObserverConstructor | undefined ;
4342
44- function tryGetWebPerformanceHooks ( ) : PerformanceHooks | undefined {
45- if ( typeof performance === "object" &&
43+ function hasRequiredAPI ( performance : Performance | undefined , PerformanceObserver : PerformanceObserverConstructor | undefined ) {
44+ return typeof performance === "object" &&
4645 typeof performance . timeOrigin === "number" &&
47- typeof performance . clearMarks === "function" &&
4846 typeof performance . mark === "function" &&
4947 typeof performance . measure === "function" &&
5048 typeof performance . now === "function" &&
51- typeof PerformanceObserver === "function" ) {
49+ typeof PerformanceObserver === "function" ;
50+ }
51+
52+ function tryGetWebPerformanceHooks ( ) : PerformanceHooks | undefined {
53+ if ( typeof performance === "object" &&
54+ typeof PerformanceObserver === "function" &&
55+ hasRequiredAPI ( performance , PerformanceObserver ) ) {
5256 return {
5357 performance,
5458 PerformanceObserver
@@ -59,16 +63,38 @@ namespace ts {
5963 function tryGetNodePerformanceHooks ( ) : PerformanceHooks | undefined {
6064 if ( typeof module === "object" && typeof require === "function" ) {
6165 try {
62- const perfHooks = require ( "perf_hooks" ) as typeof import ( "perf_hooks" ) ;
63- const { performance, PerformanceObserver } = perfHooks ;
64- if ( typeof performance === "object" &&
65- typeof performance . timeOrigin === "number" &&
66- typeof performance . clearMarks === "function" &&
67- typeof performance . mark === "function" &&
68- typeof performance . measure === "function" &&
69- typeof performance . now === "function" &&
70- typeof PerformanceObserver === "function" ) {
71- return perfHooks ;
66+ const { performance, PerformanceObserver } = require ( "perf_hooks" ) as typeof import ( "perf_hooks" ) ;
67+ if ( hasRequiredAPI ( performance , PerformanceObserver ) ) {
68+ // There is a bug in Node's performance.measure prior to 12.16.3/13.13.0 that does not
69+ // match the Web Performance API specification. Node's implementation did not allow
70+ // optional `start` and `end` arguments for `performance.measure`.
71+ // See https://github.com/nodejs/node/pull/32651 for more information.
72+ const version = new Version ( process . versions . node ) ;
73+ const range = new VersionRange ( "<12 || 13 <13.13" ) ;
74+ if ( range . test ( version ) ) {
75+ return {
76+ performance : {
77+ get timeOrigin ( ) { return performance . timeOrigin ; } ,
78+ now ( ) { return performance . now ( ) ; } ,
79+ mark ( name ) { return performance . mark ( name ) ; } ,
80+ measure ( name , start = "nodeStart" , end ?) {
81+ if ( end === undefined ) {
82+ end = "__performance.measure-fix__" ;
83+ performance . mark ( end ) ;
84+ }
85+ performance . measure ( name , start , end ) ;
86+ if ( end = "__performance.measure-fix__" ) {
87+ performance . clearMarks ( "__performance.measure-fix__" ) ;
88+ }
89+ }
90+ } ,
91+ PerformanceObserver
92+ } ;
93+ }
94+ return {
95+ performance,
96+ PerformanceObserver
97+ }
7298 }
7399 }
74100 catch {
0 commit comments