On 5/18/17 12:03 PM, Boris Zbarsky wrote:
On 5/17/17 9:22 PM, Mark Hammond wrote:
I'm wondering if there are any ideas about how to solve this optimally?

I assume https://w3c.github.io/requestidlecallback/#the-requestidlecallback-method doesn't have quite the right semantics here? That would let you run when the browser is idle, and give you some idea of how long you can run for before you should yield.

I didn't quite expect this to work, but by abusing rIC I can almost make something work - I just have the callback stash the IdleDeadline object and return immediately, but continue to refer to it anyway. eg:

let idleChecker = {
  resolved: Promise.resolve(),
  deadline: null,
  promiseIdle() {
    if (this.deadline && this.deadline.timeRemaining() > 0) {
      return this.resolved;
    }
    this.deadline = null
    return new Promise(resolve => {
      window.requestIdleCallback(deadline => {
        this.deadline = deadline;
        resolve();
      });
    })
  }
}

async function janky() {
  let start = Date.now();
  for (let i = 0; i < 1000; i++) {
    await Promise.resolve();
    await idleChecker.promiseIdle();
  }
  console.log("took", Date.now() - start)
}
janky().then(() => console.log("done"));

I 1/2 expect this to defeat the intent/implementation of rIC, but it does work, causing ~2x-5x slowdown of the loop. I wonder if this is worth experimenting with some more?

Mark


_______________________________________________
dev-platform mailing list
dev-platform@lists.mozilla.org
https://lists.mozilla.org/listinfo/dev-platform

Reply via email to