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

Avoid setTimeout in waitOnElement #321

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions resources/benchmark-runner.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,10 @@ class Page {
return new Promise((resolve) => {
const resolveIfReady = () => {
const element = this.querySelector(selector);
if (element) {
window.requestAnimationFrame(() => {
return resolve(element);
});
} else {
setTimeout(resolveIfReady, 50);
}
let callback = resolveIfReady;
Copy link
Contributor

@bgrins bgrins Nov 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a nit, but I'd find it simpler to follow if kept the basic structure in place and just replaced the setTimeout with a rAF

const resolveIfReady = () => {
  const element = this.querySelector(selector);
  if (element) {
    window.requestAnimationFrame(() => {
      resolve(element);
    });
  } else {
    window.requestAnimationFrame(resolveIfReady);
  }
};

Alternatively we could refactor this to only look for the element inside the rAF, which could potentially lead to one less rAF if the element does end up existing after the first rAF is done.

const resolveIfReady = () => {
  window.requestAnimationFrame(() => {
    const element = this.querySelector(selector);
    if (element) {
      resolve(element);
    } else {
      resolveIfReady();
    }
  });
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no real benefit to delay the lookup at this point, since the only workload where the content isn't there yet is the perf dashboard.
Eventually I'd like to remove the additional rAF delay here alltogether if it's not needed (but I prefer doing this separate to be able to easily measure the perf impact). WDYT?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in the call, we can keep the implementation as you have here

if (element)
callback = () => resolve(element);
window.requestAnimationFrame(callback);
};
resolveIfReady();
});
Expand Down