From: Evan You Date: Wed, 29 Jan 2025 07:53:20 +0000 (+0800) Subject: perf: avoid now() overhead during dev measure calls X-Git-Tag: v3.6.0-alpha.1~16^2~127 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e6ba91cface1dc9348b6d0bdb1293e00af4021a6;p=thirdparty%2Fvuejs%2Fcore.git perf: avoid now() overhead during dev measure calls --- diff --git a/packages/runtime-core/src/profiling.ts b/packages/runtime-core/src/profiling.ts index ec789de721..c14207f91e 100644 --- a/packages/runtime-core/src/profiling.ts +++ b/packages/runtime-core/src/profiling.ts @@ -5,6 +5,15 @@ import { devtoolsPerfEnd, devtoolsPerfStart } from './devtools' let supported: boolean let perf: Performance +// To avoid the overhead of repeatedly calling Date.now(), we cache +// and use the same timestamp for all event listeners attached in the same tick. +let cachedNow: number = 0 +const p = /*@__PURE__*/ Promise.resolve() +const getNow = () => + cachedNow || + (p.then(() => (cachedNow = 0)), + (cachedNow = isSupported() ? perf.now() : Date.now())) + /** * @internal */ @@ -17,7 +26,7 @@ export function startMeasure( } if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) { - devtoolsPerfStart(instance, type, isSupported() ? perf.now() : Date.now()) + devtoolsPerfStart(instance, type, getNow()) } } @@ -42,7 +51,7 @@ export function endMeasure( } if (__DEV__ || __FEATURE_PROD_DEVTOOLS__) { - devtoolsPerfEnd(instance, type, isSupported() ? perf.now() : Date.now()) + devtoolsPerfEnd(instance, type, getNow()) } } diff --git a/packages/runtime-vapor/src/renderEffect.ts b/packages/runtime-vapor/src/renderEffect.ts index 8b45b38226..0e4556fe1e 100644 --- a/packages/runtime-vapor/src/renderEffect.ts +++ b/packages/runtime-vapor/src/renderEffect.ts @@ -67,5 +67,4 @@ export function renderEffect(fn: () => void, noLifecycle = false): void { effect.run() // TODO recurse handling - // TODO measure }