From 5c5051d48affd6956c6f9aa1f21d55ddfea3b1f1 Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Wed, 25 Jan 2023 10:13:45 +0000 Subject: [PATCH] Clear performance measures when switching editor modes (#11430) GitOrigin-RevId: c1dbfa850e90b94cb5aa1c68fecd784bda099757 --- .../js/infrastructure/cm6-performance.ts | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/services/web/frontend/js/infrastructure/cm6-performance.ts b/services/web/frontend/js/infrastructure/cm6-performance.ts index bc206d46c5..2e9aa3a531 100644 --- a/services/web/frontend/js/infrastructure/cm6-performance.ts +++ b/services/web/frontend/js/infrastructure/cm6-performance.ts @@ -179,6 +179,31 @@ function calculateMax(numbers: number[]) { return numbers.reduce((a, b) => Math.max(a, b), 0) } +function clearCM6Perf(type: string) { + switch (type) { + case 'measure': + performance.clearMeasures(TIMER_MEASURE_NAME) + performance.clearMarks(TIMER_START_NAME) + performance.clearMarks(TIMER_END_NAME) + break + + case 'dom': + performance.clearMarks(TIMER_DOM_UPDATE_NAME) + break + + case 'keypress': + performance.clearMeasures(TIMER_KEYPRESS_MEASURE_NAME) + break + } +} + +// clear performance measures and marks when switching between Source and Rich Text +window.addEventListener('editor:visual-switch', () => { + clearCM6Perf('measure') + clearCM6Perf('dom') + clearCM6Perf('keypress') +}) + export function reportCM6Perf() { // Get entries triggered by keystrokes const cm6Entries = performance.getEntriesByName( @@ -186,9 +211,7 @@ export function reportCM6Perf() { 'measure' ) as PerformanceMeasure[] - performance.clearMeasures(TIMER_MEASURE_NAME) - performance.clearMarks(TIMER_START_NAME) - performance.clearMarks(TIMER_END_NAME) + clearCM6Perf('measure') const inputEvents = cm6Entries.filter(({ detail }) => isInputOrDelete(detail.userEventType) @@ -219,7 +242,7 @@ export function reportCM6Perf() { 'mark' ) as PerformanceMark[] - performance.clearMarks(TIMER_DOM_UPDATE_NAME) + clearCM6Perf('dom') let lags = 0 let nonLags = 0 @@ -257,7 +280,7 @@ export function reportCM6Perf() { const meanKeypressPaint = round(calculateMean(keypressPaintDurations), 2) - performance.clearMeasures(TIMER_KEYPRESS_MEASURE_NAME) + clearCM6Perf('keypress') let longTasks = null