performanceTimestamp: use performance.now on node

On node, `performance` is found in `require("perf_hooks")`.
This commit is contained in:
Eli Barzilay 2020-09-10 14:13:40 -04:00
parent ad96a52cc6
commit 868638ae04
2 changed files with 23 additions and 3 deletions

View File

@ -1,6 +1,26 @@
/*@internal*/
namespace ts {
declare const performance: { now?(): number } | undefined;
function tryGlobalPerformanceNow() { // browsers
if (typeof performance !== "undefined") return () => performance.now!();
}
function tryNodePerformanceNow() { // node
try {
const perf_hooks = require("perf_hooks") as typeof import("perf_hooks");
if (perf_hooks.performance) return () => perf_hooks.performance.now();
}
// eslint-disable-next-line no-empty
catch {
}
}
function tryDateNow() {
if (Date.now) return () => Date.now();
}
/** Gets a timestamp with (at least) ms resolution */
export const timestamp = typeof performance !== "undefined" && performance.now ? () => performance.now!() : Date.now ? Date.now : () => +(new Date());
}
export const timestamp: () => number =
tryGlobalPerformanceNow()
|| tryNodePerformanceNow()
|| tryDateNow()
|| (() => +(new Date()));
}

View File

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