Skip to content

Commit 4f94f87

Browse files
committed
Remove shims, workaround for bug in peformance.measure
1 parent 68806c6 commit 4f94f87

File tree

5 files changed

+45
-490
lines changed

5 files changed

+45
-490
lines changed

src/compiler/performance.ts

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,7 @@ namespace ts.performance {
4949
* @param markName The name of the mark.
5050
*/
5151
export function mark(markName: string) {
52-
if (performanceImpl) {
53-
performanceImpl.mark(markName);
54-
}
52+
performanceImpl?.mark(markName);
5553
}
5654

5755
/**
@@ -64,19 +62,7 @@ namespace ts.performance {
6462
* used.
6563
*/
6664
export function measure(measureName: string, startMarkName?: string, endMarkName?: string) {
67-
if (performanceImpl) {
68-
// NodeJS perf_hooks depends on call arity, not 'undefined' checks, so we
69-
// need to be sure we call 'measure' with the correct number of arguments.
70-
if (startMarkName === undefined) {
71-
performanceImpl.measure(measureName);
72-
}
73-
else if (endMarkName === undefined) {
74-
performanceImpl.measure(measureName, startMarkName);
75-
}
76-
else {
77-
performanceImpl.measure(measureName, startMarkName, endMarkName);
78-
}
79-
}
65+
performanceImpl?.measure(measureName, startMarkName, endMarkName);
8066
}
8167

8268
/**
@@ -103,9 +89,7 @@ namespace ts.performance {
10389
* @param cb The action to perform for each measure
10490
*/
10591
export function forEachMeasure(cb: (measureName: string, duration: number) => void) {
106-
perfEntryList?.getEntriesByType("measure").forEach(entry => {
107-
cb(entry.name, entry.duration);
108-
});
92+
perfEntryList?.getEntriesByType("measure").forEach(({ name, duration }) => { cb(name, duration); });
10993
}
11094

11195
/**
@@ -118,7 +102,7 @@ namespace ts.performance {
118102
/** Enables (and resets) performance measurements for the compiler. */
119103
export function enable() {
120104
if (!performanceImpl) {
121-
perfHooks ||= tryGetNativePerformanceHooks() || ShimPerformance?.createPerformanceHooksShim(timestamp);
105+
perfHooks ||= tryGetNativePerformanceHooks();
122106
if (!perfHooks) return false;
123107
perfObserver ||= new perfHooks.PerformanceObserver(list => perfEntryList = list);
124108
perfObserver.observe({ entryTypes: ["mark", "measure"] });

src/compiler/performanceCore.ts

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

src/shims/performanceShims.ts

Lines changed: 0 additions & 203 deletions
This file was deleted.

src/shims/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55
},
66
"files": [
77
"collectionShims.ts",
8-
"performanceShims.ts",
98
]
109
}

0 commit comments

Comments
 (0)