diff --git a/src/compiler/performance.ts b/src/compiler/performance.ts index 9f7d19c95df75..e1e26831cb18f 100644 --- a/src/compiler/performance.ts +++ b/src/compiler/performance.ts @@ -3,7 +3,6 @@ namespace ts.performance { let perfHooks: PerformanceHooks | undefined; let perfObserver: PerformanceObserver | undefined; - let perfEntryList: PerformanceObserverEntryList | undefined; // when set, indicates the implementation of `Performance` to use for user timing. // when unset, indicates user timing is unavailable or disabled. let performanceImpl: Performance | undefined; @@ -42,6 +41,8 @@ namespace ts.performance { } export const nullTimer: Timer = { enter: noop, exit: noop }; + const counts = new Map(); + const durations = new Map(); /** * Marks a performance event. @@ -71,7 +72,7 @@ namespace ts.performance { * @param markName The name of the mark. */ export function getCount(markName: string) { - return perfEntryList?.getEntriesByName(markName, "mark").length || 0; + return counts.get(markName) || 0; } /** @@ -80,7 +81,7 @@ namespace ts.performance { * @param measureName The name of the measure whose durations should be accumulated. */ export function getDuration(measureName: string) { - return perfEntryList?.getEntriesByName(measureName, "measure").reduce((a, entry) => a + entry.duration, 0) || 0; + return durations.get(measureName) || 0; } /** @@ -89,7 +90,7 @@ namespace ts.performance { * @param cb The action to perform for each measure */ export function forEachMeasure(cb: (measureName: string, duration: number) => void) { - perfEntryList?.getEntriesByType("measure").forEach(({ name, duration }) => { cb(name, duration); }); + durations.forEach((duration, measureName) => cb(measureName, duration)); } /** @@ -104,7 +105,7 @@ namespace ts.performance { if (!performanceImpl) { perfHooks ||= tryGetNativePerformanceHooks(); if (!perfHooks) return false; - perfObserver ||= new perfHooks.PerformanceObserver(list => perfEntryList = list); + perfObserver ||= new perfHooks.PerformanceObserver(updateStatisticsFromList); perfObserver.observe({ entryTypes: ["mark", "measure"] }); performanceImpl = perfHooks.performance; } @@ -115,5 +116,16 @@ namespace ts.performance { export function disable() { perfObserver?.disconnect(); performanceImpl = undefined; + counts.clear(); + durations.clear(); + } + + function updateStatisticsFromList(list: PerformanceObserverEntryList) { + for (const mark of list.getEntriesByType("mark")) { + counts.set(mark.name, (counts.get(mark.name) || 0) + 1); + } + for (const measure of list.getEntriesByType("measure")) { + durations.set(measure.name, (durations.get(measure.name) || 0) + measure.duration); + } } } diff --git a/src/compiler/performanceCore.ts b/src/compiler/performanceCore.ts index 69afa9388137a..acec7f4ce5700 100644 --- a/src/compiler/performanceCore.ts +++ b/src/compiler/performanceCore.ts @@ -71,7 +71,7 @@ namespace ts { // optional `start` and `end` arguments for `performance.measure`. // See https://github.com/nodejs/node/pull/32651 for more information. const version = new Version(process.versions.node); - const range = new VersionRange("<12 || 13 <13.13"); + const range = new VersionRange("<12.16.3 || 13 <13.13"); if (range.test(version)) { return { performance: { @@ -84,7 +84,7 @@ namespace ts { performance.mark(end); } performance.measure(name, start, end); - if (end = "__performance.measure-fix__") { + if (end === "__performance.measure-fix__") { performance.clearMarks("__performance.measure-fix__"); } } diff --git a/src/compiler/tsconfig.json b/src/compiler/tsconfig.json index 623a3aede4d9d..ed64c2c26d593 100644 --- a/src/compiler/tsconfig.json +++ b/src/compiler/tsconfig.json @@ -13,10 +13,10 @@ "corePublic.ts", "core.ts", "debug.ts", + "semver.ts", "performanceCore.ts", "performance.ts", "perfLogger.ts", - "semver.ts", "tracing.ts", "types.ts",