From 98e168a0afc33726e68dea893541ce5b507abcdf Mon Sep 17 00:00:00 2001 From: Alf Eaton Date: Tue, 15 Nov 2022 13:26:41 +0000 Subject: [PATCH] Refactor performance measurement (#10410) GitOrigin-RevId: c63b2353aac04c124f4aa499910a823f3796982c --- .../js/infrastructure/cm6-performance.ts | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/services/web/frontend/js/infrastructure/cm6-performance.ts b/services/web/frontend/js/infrastructure/cm6-performance.ts index 13a0ac5aa6..2a58957675 100644 --- a/services/web/frontend/js/infrastructure/cm6-performance.ts +++ b/services/web/frontend/js/infrastructure/cm6-performance.ts @@ -63,38 +63,37 @@ function isKeypress(userEventType: string | undefined) { ) } -export function timedDispatch() { +export function dispatchTimer(): { + start: (tr: Transaction) => void + end: (tr: Transaction, view: EditorView) => void +} { + if (!performanceOptionsSupport) { + return { start: () => {}, end: () => {} } + } + let userEventsSinceDomUpdateCount = 0 let keypressesSinceDomUpdateCount = 0 const unpaintedKeypressStartTimes: number[] = [] - return ( - view: EditorView, - tr: Transaction, - dispatchFn: (tr: Transaction) => void - ) => { - if (!performanceOptionsSupport) { - dispatchFn(tr) - return - } - - performance.mark(TIMER_START_NAME) - + const start = (tr: Transaction) => { const userEventType = tr.annotation(Transaction.userEvent) - const eventIsKeypress = isKeypress(userEventType) - if (eventIsKeypress) { + if (isKeypress(userEventType)) { unpaintedKeypressStartTimes.push(performance.now()) } - dispatchFn(tr) + performance.mark(TIMER_START_NAME) + } + const end = (tr: Transaction, view: EditorView) => { performance.mark(TIMER_END_NAME) + const userEventType = tr.annotation(Transaction.userEvent) + if (isInputOrDelete(userEventType)) { ++userEventsSinceDomUpdateCount - if (eventIsKeypress) { + if (isKeypress(userEventType)) { ++keypressesSinceDomUpdateCount } @@ -130,6 +129,8 @@ export function timedDispatch() { latestDocLength = tr.state.doc.length } + + return { start, end } } function calculateMean(durations: number[]) {