Skip to content

Commit 868638a

Browse files
committed
performanceTimestamp: use performance.now on node
On node, `performance` is found in `require("perf_hooks")`.
1 parent ad96a52 commit 868638a

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

Diff for: src/compiler/performanceTimestamp.ts

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
/*@internal*/
22
namespace ts {
33
declare const performance: { now?(): number } | undefined;
4+
function tryGlobalPerformanceNow() { // browsers
5+
if (typeof performance !== "undefined") return () => performance.now!();
6+
}
7+
function tryNodePerformanceNow() { // node
8+
try {
9+
const perf_hooks = require("perf_hooks") as typeof import("perf_hooks");
10+
if (perf_hooks.performance) return () => perf_hooks.performance.now();
11+
}
12+
// eslint-disable-next-line no-empty
13+
catch {
14+
}
15+
}
16+
function tryDateNow() {
17+
if (Date.now) return () => Date.now();
18+
}
419
/** Gets a timestamp with (at least) ms resolution */
5-
export const timestamp = typeof performance !== "undefined" && performance.now ? () => performance.now!() : Date.now ? Date.now : () => +(new Date());
6-
}
20+
export const timestamp: () => number =
21+
tryGlobalPerformanceNow()
22+
|| tryNodePerformanceNow()
23+
|| tryDateNow()
24+
|| (() => +(new Date()));
25+
26+
}

Diff for: src/harness/virtualFileSystemWithWatch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ interface Array<T> { length: number; [n: number]: T; }`
12051205
function baselineOutputs(baseline: string[], output: readonly string[], start: number, end = output.length) {
12061206
let baselinedOutput: string[] | undefined;
12071207
for (let i = start; i < end; i++) {
1208-
(baselinedOutput ||= []).push(output[i].replace(/Elapsed::\s[0-9]+ms/g, "Elapsed:: *ms"));
1208+
(baselinedOutput ||= []).push(output[i].replace(/Elapsed::\s[0-9]+(?:\.\d+)?ms/g, "Elapsed:: *ms"));
12091209
}
12101210
if (baselinedOutput) baseline.push(baselinedOutput.join(""));
12111211
}

0 commit comments

Comments
 (0)