Skip to content

Commit c5800d1

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

File tree

7 files changed

+47
-492
lines changed

7 files changed

+47
-492
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: 42 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,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 {

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
}

src/testRunner/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
"unittests/semver.ts",
7676
"unittests/createMapShim.ts",
7777
"unittests/createSetShim.ts",
78-
"unittests/createPerformanceHooksShim.ts",
7978
"unittests/transform.ts",
8079
"unittests/config/commandLineParsing.ts",
8180
"unittests/config/configurationExtension.ts",

0 commit comments

Comments
 (0)