[DevTools] Fix high CPU usage on pages without React (#35515)#344
[DevTools] Fix high CPU usage on pages without React (#35515)#344everettbu wants to merge 2 commits into
Conversation
Stop polling for React after 10 attempts (~5 seconds) to prevent infinite loop of 'hello' messages on pages without React. Fixes #35515
Greptile SummaryReplaced CPU-intensive polling with an event-driven handshake between the content script and backend manager. The backend manager now signals readiness via a data attribute and custom event, eliminating the 500ms polling loop that caused high CPU usage on non-React pages.
Confidence Score: 4/5
Important Files Changed
|
Replaces the polling mechanism in proxy.js with an event-driven approach. backendManager.js now dispatches a 'react-devtools-ready' event and sets a data attribute when initialized. This eliminates CPU usage on non-React pages by removing the need for a polling loop.
| const onBackendReady = () => { | ||
| window.removeEventListener('react-devtools-ready', onBackendReady); | ||
| sayHelloToBackendManager(); | ||
| }; | ||
|
|
||
| if (document.documentElement.getAttribute('data-react-devtools-ready')) { | ||
| onBackendReady(); | ||
| } else { | ||
| window.addEventListener('react-devtools-ready', onBackendReady); | ||
| } |
There was a problem hiding this comment.
logic: The react-devtools-ready event listener is never cleaned up on pages without React. When backendManager.js is not injected (non-React pages), onBackendReady never fires, so the listener removal on line 28 never executes. This creates a memory leak across page navigations.
Add cleanup in the pagehide handler:
window.addEventListener('pagehide', function ({target}) {
if (target !== window.document) {
return;
}
window.removeEventListener('react-devtools-ready', onBackendReady);
delete window.__REACT_DEVTOOLS_PROXY_INJECTED__;
});
Did you test navigating between non-React pages to verify the listener cleanup behavior?
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/react-devtools-extensions/src/contentScripts/proxy.js
Line: 27:36
Comment:
**logic:** The `react-devtools-ready` event listener is never cleaned up on pages without React. When `backendManager.js` is not injected (non-React pages), `onBackendReady` never fires, so the listener removal on line 28 never executes. This creates a memory leak across page navigations.
Add cleanup in the `pagehide` handler:
```
window.addEventListener('pagehide', function ({target}) {
if (target !== window.document) {
return;
}
window.removeEventListener('react-devtools-ready', onBackendReady);
delete window.__REACT_DEVTOOLS_PROXY_INJECTED__;
});
```
Did you test navigating between non-React pages to verify the listener cleanup behavior?
How can I resolve this? If you propose a fix, please make it concise.|
This pull request has been automatically marked as stale. If this pull request is still relevant, please leave any comment (for example, "bump"), and we'll keep it open. We are sorry that we haven't been able to prioritize reviewing it yet. Your contribution is very much appreciated. |
|
Closing this pull request after a prolonged period of inactivity. If this issue is still present in the latest release, please ask for this pull request to be reopened. Thank you! |
Mirror of facebook/react#35516
Original author: CarlosEduJs
Summary
Fixes high CPU usage in React DevTools when browsing pages without React (e.g., Google Search results).
Motivation: The content script was polling indefinitely with setInterval, sending "hello" messages every 500ms to detect React. On pages without React, this loop never stopped, causing high CPU usage and battery drain.
Solution: Added a maximum retry limit of 10 attempts (~5 seconds). After this timeout, the polling stops automatically on pages without React, while still allowing enough time for slow-loading React apps to initialize.
How did you test this change?
Manual Testing
Built and tested the extension on both Chrome and Firefox:
Chrome:
Firefox:
Results
Before fix:
After fix:
Screenshot:

Checklist
Related Issue