From 65c561e195285e1f1ea59bec72899dabf08502a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Mon, 20 Apr 2026 09:31:43 -0700 Subject: [PATCH] PerformanceObserver: default durationThreshold to 104ms (#56506) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/56506 React Native's `PerformanceObserver` previously defaulted `durationThreshold` to 0 for `event` entries, while the W3C Event Timing spec (https://www.w3.org/TR/event-timing/#sec-modifications-perf-timeline) mandates a default of 104ms. The previous default flooded observers with short events (clicks, pointer up/down) that are not interesting for responsiveness measurement. This change brings React Native in line with the spec by making 104ms the default. The default is applied at the JSI bridge boundary in `NativePerformance.cpp` (the JS API boundary on the C++ side) when JS does not provide an explicit `durationThreshold`. The C++ public observer API (`PerformanceObserverObserveSingleOptions::durationThreshold`) and the global event buffer (`eventBuffer_.durationThreshold`) keep their `HighResDuration::zero()` default — applying 104ms to the global buffer would drop sub-104ms events at the source, breaking observers that explicitly request `durationThreshold: 0`. Changes in `react-native`: - `NativePerformance.cpp`: apply 104ms default at the JSI bridge boundary. - `PerformanceApiExample.js` (rn-tester): pass `durationThreshold: 0` to preserve previous behavior. - `EventTimingAPI-itest.js`: migrate 4 existing call sites to `{type: 'event', durationThreshold: 0}` and add a new test verifying the 104ms default. Sites using `{entryTypes: ['event']}` were converted to `{type: 'event', durationThreshold: 0}` since the spec (and existing JS validation in `PerformanceObserver.js`) disallows `durationThreshold` together with `entryTypes`. Changelog: [General][Fixed] - PerformanceObserver: `observe({type: 'event'})` now correctly defaults `durationThreshold` to 104ms per the W3C Event Timing spec instead of reporting all events. Reviewed By: hoxyq Differential Revision: D101629586 fbshipit-source-id: 897914e8582a3d16db9e9388606be5d0aebd19ab --- .../webperformance/NativePerformance.cpp | 11 +++- .../__tests__/EventTimingAPI-itest.js | 54 +++++++++++++++++-- .../Performance/PerformanceApiExample.js | 2 +- 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.cpp b/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.cpp index c1c8d70167c..e3d7b7f440c 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.cpp +++ b/packages/react-native/ReactCommon/react/nativemodule/webperformance/NativePerformance.cpp @@ -34,6 +34,15 @@ namespace facebook::react { namespace { +/** + * Default `durationThreshold` (in milliseconds) applied to `event` entries when + * the JS API does not provide an explicit value. Per the W3C Event Timing + * spec, observers for `event` entries default to a 104ms threshold: + * https://www.w3.org/TR/event-timing/#sec-modifications-perf-timeline + */ +constexpr HighResDuration DEFAULT_EVENT_DURATION_THRESHOLD = + HighResDuration::fromMilliseconds(104); + class PerformanceObserverWrapper : public jsi::NativeState { public: explicit PerformanceObserverWrapper( @@ -337,7 +346,7 @@ void NativePerformance::observe( } auto durationThreshold = - options.durationThreshold.value_or(HighResDuration::zero()); + options.durationThreshold.value_or(DEFAULT_EVENT_DURATION_THRESHOLD); // observer of type multiple if (options.entryTypes.has_value()) { diff --git a/packages/react-native/src/private/webapis/performance/__tests__/EventTimingAPI-itest.js b/packages/react-native/src/private/webapis/performance/__tests__/EventTimingAPI-itest.js index 398ecd712ce..ab89fd1d4f3 100644 --- a/packages/react-native/src/private/webapis/performance/__tests__/EventTimingAPI-itest.js +++ b/packages/react-native/src/private/webapis/performance/__tests__/EventTimingAPI-itest.js @@ -38,7 +38,7 @@ describe('Event Timing API', () => { const callback = jest.fn(); const observer = new PerformanceObserver(callback); - observer.observe({entryTypes: ['event']}); + observer.observe({type: 'event', durationThreshold: 0}); const root = Fantom.createRoot(); Fantom.runTask(() => { @@ -79,7 +79,7 @@ describe('Event Timing API', () => { const callback = jest.fn(); const observer = new PerformanceObserver(callback); - observer.observe({entryTypes: ['event']}); + observer.observe({type: 'event', durationThreshold: 0}); const SIMULATED_PROCESSING_DELAY = 50; @@ -132,7 +132,7 @@ describe('Event Timing API', () => { const callback = jest.fn(); const observer = new PerformanceObserver(callback); - observer.observe({entryTypes: ['event']}); + observer.observe({type: 'event', durationThreshold: 0}); function MyComponent() { const [count, setCount] = useState(0); @@ -188,7 +188,7 @@ describe('Event Timing API', () => { const callback = jest.fn(); const observer = new PerformanceObserver(callback); - observer.observe({entryTypes: ['event']}); + observer.observe({type: 'event', durationThreshold: 0}); let eventTimeStamp; @@ -341,6 +341,52 @@ describe('Event Timing API', () => { expect(callback).toHaveBeenCalledTimes(1); }); + it('defaults to 104ms for event entries per W3C spec', () => { + const callback = jest.fn(); + + const observer = new PerformanceObserver(callback); + observer.observe({type: 'event'}); + + let forceDelay = false; + + function MyComponent() { + const [count, setCount] = useState(0); + + return ( + { + if (forceDelay) { + sleep(104); + } + setCount(count + 1); + }}> + {count} + + ); + } + + const root = Fantom.createRoot(); + Fantom.runTask(() => { + root.render(); + }); + + const element = nullthrows( + root.document.documentElement.firstElementChild, + ); + + expect(callback).not.toHaveBeenCalled(); + + Fantom.dispatchNativeEvent(element, 'click'); + + expect(callback).toHaveBeenCalledTimes(0); + + forceDelay = true; + + Fantom.dispatchNativeEvent(element, 'click'); + + expect(callback).toHaveBeenCalledTimes(1); + }); + it('throws when used together with `entryTypes`', () => { const observer = new PerformanceObserver(() => {}); diff --git a/packages/rn-tester/js/examples/Performance/PerformanceApiExample.js b/packages/rn-tester/js/examples/Performance/PerformanceApiExample.js index 442f6a73611..24c9e1bc9be 100644 --- a/packages/rn-tester/js/examples/Performance/PerformanceApiExample.js +++ b/packages/rn-tester/js/examples/Performance/PerformanceApiExample.js @@ -143,7 +143,7 @@ function PerformanceObserverEventTimingExample(): React.Node { setEntries(newEntries); }); - observer.observe({type: 'event'}); + observer.observe({type: 'event', durationThreshold: 0}); return () => observer.disconnect(); }, []);