@@ -9,7 +9,6 @@ namespace ts {
9
9
}
10
10
11
11
export interface Performance {
12
- clearMarks ( name ?: string ) : void ;
13
12
mark ( name : string ) : void ;
14
13
measure ( name : string , startMark ?: string , endMark ?: string ) : void ;
15
14
now ( ) : number ;
@@ -41,14 +40,20 @@ namespace ts {
41
40
declare const performance : Performance | undefined ;
42
41
declare const PerformanceObserver : PerformanceObserverConstructor | undefined ;
43
42
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" &&
46
46
typeof performance . timeOrigin === "number" &&
47
- typeof performance . clearMarks === "function" &&
48
47
typeof performance . mark === "function" &&
49
48
typeof performance . measure === "function" &&
50
49
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 ) ) {
52
57
return {
53
58
performance,
54
59
PerformanceObserver
@@ -59,16 +64,38 @@ namespace ts {
59
64
function tryGetNodePerformanceHooks ( ) : PerformanceHooks | undefined {
60
65
if ( typeof module === "object" && typeof require === "function" ) {
61
66
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
+ } ;
72
99
}
73
100
}
74
101
catch {
0 commit comments