Skip to content

Commit 5284555

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

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

src/compiler/performanceTimestamp.ts

Lines changed: 22 additions & 2 deletions
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+
}

0 commit comments

Comments
 (0)