perf: avoid now() overhead during dev measure calls

This commit is contained in:
Evan You 2025-01-29 15:53:20 +08:00
parent 1e9062c075
commit e6ba91cfac
No known key found for this signature in database
GPG Key ID: 00E9AB7A6704CE0A
2 changed files with 11 additions and 3 deletions

View File

@ -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())
}
}

View File

@ -67,5 +67,4 @@ export function renderEffect(fn: () => void, noLifecycle = false): void {
effect.run()
// TODO recurse handling
// TODO measure
}