@@ -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,20 @@ 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+ // eslint-disable-next-line @typescript-eslint/naming-convention
44+ function hasRequiredAPI ( performance : Performance | undefined , PerformanceObserver : PerformanceObserverConstructor | undefined ) {
45+ return typeof performance === "object" &&
4646 typeof performance . timeOrigin === "number" &&
47- typeof performance . clearMarks === "function" &&
4847 typeof performance . mark === "function" &&
4948 typeof performance . measure === "function" &&
5049 typeof performance . now === "function" &&
51- typeof PerformanceObserver === "function" ) {
50+ typeof PerformanceObserver === "function" ;
51+ }
52+
53+ function tryGetWebPerformanceHooks ( ) : PerformanceHooks | undefined {
54+ if ( typeof performance === "object" &&
55+ typeof PerformanceObserver === "function" &&
56+ hasRequiredAPI ( performance , PerformanceObserver ) ) {
5257 return {
5358 performance,
5459 PerformanceObserver
@@ -59,16 +64,38 @@ namespace ts {
5964 function tryGetNodePerformanceHooks ( ) : PerformanceHooks | undefined {
6065 if ( typeof module === "object" && typeof require === "function" ) {
6166 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 ;
67+ const { performance, PerformanceObserver } = require ( "perf_hooks" ) as typeof import ( "perf_hooks" ) ;
68+ if ( hasRequiredAPI ( performance , PerformanceObserver ) ) {
69+ // There is a bug in Node's performance.measure prior to 12.16.3/13.13.0 that does not
70+ // match the Web Performance API specification. Node's implementation did not allow
71+ // optional `start` and `end` arguments for `performance.measure`.
72+ // See https://github.com/nodejs/node/pull/32651 for more information.
73+ const version = new Version ( process . versions . node ) ;
74+ const range = new VersionRange ( "<12 || 13 <13.13" ) ;
75+ 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+ }
90+ }
91+ } ,
92+ PerformanceObserver
93+ } ;
94+ }
95+ return {
96+ performance,
97+ PerformanceObserver
98+ } ;
7299 }
73100 }
74101 catch {
0 commit comments