@@ -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,19 @@ 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
+ function hasRequiredAPI ( performance : Performance | undefined , PerformanceObserver : PerformanceObserverConstructor | undefined ) {
44
+ return typeof performance === "object" &&
46
45
typeof performance . timeOrigin === "number" &&
47
- typeof performance . clearMarks === "function" &&
48
46
typeof performance . mark === "function" &&
49
47
typeof performance . measure === "function" &&
50
48
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 ) ) {
52
56
return {
53
57
performance,
54
58
PerformanceObserver
@@ -59,16 +63,38 @@ namespace ts {
59
63
function tryGetNodePerformanceHooks ( ) : PerformanceHooks | undefined {
60
64
if ( typeof module === "object" && typeof require === "function" ) {
61
65
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
+ }
72
98
}
73
99
}
74
100
catch {
0 commit comments