Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【Discuss & May Performance Issue】whenIdle add never removed visibilitychange event listener #534

Closed
youngBrain1893 opened this issue Sep 23, 2024 · 1 comment

Comments

@youngBrain1893
Copy link

youngBrain1893 commented Sep 23, 2024

I was just tried to collect LCP metric for my project,and when I just add onLCP for my project, I got an performance issue at the Performance monitor panel:the JS event listeners increased at every operate on the page
and I tried to debug the code,and I got the code onLCP.ts#L103-L108 may case the performance issue

['keydown', 'click'].forEach((type) => {
        // Wrap in a setTimeout so the callback is run in a separate task
        // to avoid extending the keyboard/click handler to reduce INP impact
        // https://github.com/GoogleChrome/web-vitals/issues/383
        addEventListener(type, () => whenIdle(stopListening), true);
});
  1. it add click and keydown event, and the handler was wrapped with whenIdle
  2. whenIdle will call onHidden which will add visibilitychange listener when the page's visibilityState is not 'hidden', and will never be removed.
  3. each time the page (or tab for chrome) visibility change(hidden -> visible), the listeners will execute
click/keydown -> whenIdle process -> add visiblitychange listener(always increase never remove)

for the onINP each time the PerformanceObserve.observe('event') was fired, the handle function(handleEntries)call whenIdle which add a visiblitychange listener

It's a performance issue or designed?

@tunetheweb
Copy link
Member

Unless I'm missing something, I think you might be right! I've raised #537 to fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants