BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Facebook Proposes New IsInputPending API for Faster Input Event Processing

Facebook Proposes New IsInputPending API for Faster Input Event Processing

This item in japanese

Bookmarks

Facebook recently announced its first major browser API contribution. The new isInputPending API aims to shorten the time between a user input and its processing by the browser, and to increase the user experience of highly interactive applications.

The new navigator.scheduling.isInputPending browser API proposal exposes a function isInputPending which will return true if the browser has an input event scheduled for dispatching. API consumers may specify specific event types (like touch or key events) to be checked by calling the function with the appropriate parameters.

In the announcing blog post, Facebook’s Nate Schloss and Andrew Comminos give the following example of usage, where a script includes a logic by which it interrupts its computation to let the browser handle inputs:

while (workQueue.length > 0) {
  if (navigator.scheduling.isInputPending(['mousedown', 'mouseup', 'keydown', 'keyup'])) {
    // Stop doing work if we think we'll start receiving a mouse or key event.
    break;
  }
  let job = workQueue.shift();
  job.execute();
}

With the proposed API, the script only yields execution to the browser when it has a good reason to. A common alternative to running long scripts on the main thread while mitigating the impact on interactivity involves yielding back to the browser at arbitrarily defined intervals. However, yielding execution to the browser when there are no inputs to process implies some inefficiency, in particular for frameworks which need to load large scripts on a page. Facebook explains:

If we yield too often, the page loads too slowly. If we yield less often, it takes longer for the browser to respond to user events, and people get frustrated.

Another alternative is to use the Background Tasks API, in particular the requestIdleCallback() API, to run scripts when the browser is idle. However, this API is designed for tasks which do not have a high priority. As the proposal explains, this approach is also fraught with difficulties:

In the example of registering behavior for a variety of UI components, we can’t wait for an idle period before continuing to register additional UI components.

In that context, the new API seeks to provide a cooperative scheduling solution which allows task to complete as fast as possible while letting the browsers process input events in a way that users do not notice a lag between an input and its perceived effects (generally changes to the DOM).

The new isInputPending API is included in Chrome 74, as an origin trial. Facebook mentions:

The origin trial will allow us to get a sense of how important the API is to developers, which is important information for all browser vendors on the web and is will shape our future conversations about this API.

The new API has been divulgated through the W3C’s Web Platform Incubator Community Group (WICG), a platform supported by major browser vendors such as Microsoft, Apple, Google, and Mozilla, to have a dialog about new features. The corresponding WICG proposal details the rationale behind the API together with the constraints it has to meet and the privacy and security issues it creates.

If feedback is positive, the new API could become fully available in Chrome and other browsers. Facebook hints that additional APIs could be contributed down the line:

isInputPending is now part of a larger effort to build scheduling primitives into the web. Eventually, we hope to see browser tools that allow developers to integrate deeper into the browser’s task queue and even let developers give the browser insight into the priorities of various network requests and tasks.

Developers may register and share feedback on the origin trial.

Rate this Article

Adoption
Style

BT