fn

usePerformanceObserver

v0.0.14testeddemo

Observe performance metrics via PerformanceObserver. The observer is (re)created only when activation changes, and can be paused, resumed, or permanently stopped. SSR-safe: nothing runs until mounted in a supporting environment.

Examples

ts
usePerformanceObserver((list) => {
  console.log(list.getEntries());
}, { entryTypes: ['paint', 'measure'] });
ts
const { pause, resume } = usePerformanceObserver(onEntries, {
  entryTypes: ['longtask'],
  buffered: true,
});

Demo

Loading demo…

Signature

ts
export function usePerformanceObserver(
  callback: PerformanceObserverCallback,
  options: UsePerformanceObserverOptions ={ ... }

Parameters

ParameterTypeDescription
callbackPerformanceObserverCallbackInvoked with the observed entries and observer
options?UsePerformanceObserverOptionsObserver init (entryTypes/type/buffered) plus immediate

Returns

UsePerformanceObserverReturnisSupported, isActive, pause, resume, stop, and takeRecords
PropertyTypeDescription
isSupportedReadonly<Ref<boolean>>Whether PerformanceObserver is supported in the current environment
isActiveReadonly<Ref<boolean>>Whether the observer is currently active
resume() => voidResume observing after a pause
pause() => voidTemporarily stop observing (disconnects the observer) while keeping the activation watcher alive, so observing can be resumed later
stop() => voidPermanently stop observing and tear down the activation watcher
takeRecords() => PerformanceEntryList | undefinedSynchronously return the current list of buffered performance entries and clear the observer's buffer. Returns undefined when not observing.