W3C

Long Tasks API 1

Editor’s Draft,

This version:
https://w3c.github.io/longtasks/
Test Suite:
http://w3c-test.org/longtask-timing/
Issue Tracking:
GitHub
Inline In Spec
Editors:
(Google)
(Google)
(Google)

Abstract

This document defines an API that web page authors can use to detect presence of "long tasks" that monopolize the UI thread for extended periods of time and block other critical tasks from being executed - e.g. reacting to user input.

Status of this document

This section describes the status of this document at the time of its publication. Other documents may supersede this document. A list of current W3C publications and the latest revision of this technical report can be found in the W3C technical reports index at https://www.w3.org/TR/.

This document was published by the Web Performance Working Group as an Editors Draft. This document is intended to become a W3C Recommendation.

If you wish to make comments regarding this document, please send them to public-web-perf@w3.org (subscribe, archives) with [LongTasks] at the start of your email’s subject.

Feedback and comments on this specification are welcome, please send them to public-web-perf@w3.org (subscribe, archives) with [longtasks] at the start of your email’s subject.

Publication as an Editors Draft does not imply endorsement by the W3C Membership. This is a draft document and may be updated, replaced or obsoleted by other documents at any time. It is inappropriate to cite this document as other than work in progress.

This document was produced by a group operating under the W3C Patent Policy. W3C maintains a public list of any patent disclosures made in connection with the deliverables of the group; that page also includes instructions for disclosing a patent. An individual who has actual knowledge of a patent which the individual believes contains Essential Claim(s) must disclose the information in accordance with section 6 of the W3C Patent Policy.

This document is governed by the 15 September 2020 W3C Process Document.

1. Introduction

As the page is loading and while the user is interacting with the page afterwards, both the application and browser queue various events that are then executed by the browser -- e.g. user agent schedules input events based on user’s activity, the application schedules callbacks for requestAnimationFrame and other callbacks, etc. Once in the queue, the browser dequeues these events one-by-one and executes them.

However, some tasks can take a long time (multiple frames) and if/when that happens, the UI thread may become blocked and block all other tasks as well. To the user, this is commonly visible as a "locked up" page where the browser is unable to respond to user input; this is a major source of bad user experience on the web today:

Delayed "time to Interactive":

while the page is loading, or even completely visually rendered, long tasks often tie up the main thread and prevent the user from interacting with the page. Poorly designed third-party content is frequently the culprit.

High/variable input latency:

critical user-interaction events (e.g. tap, click, scroll, wheel, etc.) are queued behind long tasks which yields janky and unpredictable user experience.

High/variable event handling latency:

like input, processing event callbacks (e.g. onload events, etc.) delay application updates.

Janky animations and scrolling:

some animation and scrolling interactions require coordination between compositor and main threads; if a long task is blocking the main thread it can affect responsiveness of animations and scrolling.

Some applications (and RUM vendors) are already attempting to identify and track cases where "long tasks" happen. For example, one known pattern is to install a ~short periodic timer and inspect the elapsed time between the successive expirations: if the elapsed time is greater than the timer period, then there is high likelihood that one or more long tasks have delayed execution of the event loop. This approach mostly works but has several bad performance implications: by polling to detect long tasks, the application prevents quiescence and long idle blocks (see requestIdleCallback); it’s bad for battery life; there is no way to know what is causing the delay (e.g. first party or third party code).

The RAIL performance model suggests that applications should respond to user input in less than 100ms (for touch move and scrolling, the threshold is 16ms). The goal of this API is to surface notifications about tasks that may prevent the application from hitting these targets. This API surfaces tasks that take 50ms or more. A website without these tasks should respond to user input in under 100ms: it will take less than 50ms to finish the task that is being executed when the user input is received and less than 50ms to execute the task to react to such user input.

1.1. Usage Example

const observer = new PerformanceObserver(function(list) {
    for (const entry of list.getEntries()) {
        // Process long task notifications:
        // report back for analytics and monitoring
        // ...
    }
});
// Register observer for previous and future long task notifications.
observer.observe({type: "longtask", buffered: true});
// Long script execution after this will result in queueing
// and receiving "longtask" entries in the observer.

2. Terminology

Long task refers to any of the following occurrences whose duration exceeds 50ms:

Culprit browsing context container refers to the browsing context container (iframe, object, etc.) that is being implicated, on the whole, for a long task.

Attribution refers to identifying the type of work (such as script, layout etc.) that contributed significantly to the long task, as well as identifying which culprit browsing context container is responsible for that work.

3. Long Task Timing

Long Task timing involves the following new interfaces:

3.1. PerformanceLongTaskTiming interface

PerformanceLongTaskTiming

In only one current engine.

FirefoxNoneSafari?Chrome58+
OperaYesEdge79+
Edge (Legacy)NoneIENone
Firefox for AndroidNoneiOS Safari?Chrome for Android58+Android WebView58+Samsung Internet7.0+Opera MobileYes
[Exposed=Window]
interface PerformanceLongTaskTiming : PerformanceEntry {
    readonly attribute FrozenArray<TaskAttributionTiming> attribution;
    [Default] object toJSON();
};

The values of the attributes of a PerformanceLongTaskTiming are set in the processing model in § 4.1 Report long tasks. The following provides an informative summary of how they will be set.

The name attribute’s getter will return one of the following strings:

"unknown"

The long task originated from work that the user agent performed outside of the event loop.

"self"

The long task originated from an event loop task within this browsing context.

"same-origin-ancestor"

The long task originated from an event loop task within a same-origin ancestor browsing context.

"same-origin-descendant"

The long task originated from an event loop task within a same-origin descendant browsing context.

"same-origin"

The long task originated from an event loop task within a same-origin browsing context that is not an ancestor or descendant.

"cross-origin-ancestor"

The long task originated from an event loop task within a cross-origin ancestor browsing context.

"cross-origin-descendant"

The long task originated from an event loop task within a cross-origin descendant browsing context.

"cross-origin-unreachable"

The long task originated from an event loop task within a cross-origin browsing context that is not an ancestor or descendant.

"multiple-contexts"

The long task originated from an event loop task involving multiple browsing contexts.

The entryType attribute’s getter will return "longtask".

The startTime attribute’s getter will return a DOMHighResTimeStamp of when the task started.

The duration attribute’s getter will return a DOMHighResTimeStamp equal to the elapsed time between the start and end of task.

PerformanceLongTaskTiming/attribution

In only one current engine.

FirefoxNoneSafari?Chrome58+
OperaYesEdge79+
Edge (Legacy)NoneIENone
Firefox for AndroidNoneiOS Safari?Chrome for Android58+Android WebView58+Samsung Internet7.0+Opera MobileYes

The attribution attribute’s getter will return a frozen array of TaskAttributionTiming entries.

3.2. TaskAttributionTiming interface

TaskAttributionTiming

In only one current engine.

FirefoxNoneSafari?Chrome58+
OperaYesEdge79+
Edge (Legacy)NoneIENone
Firefox for AndroidNoneiOS Safari?Chrome for Android58+Android WebView58+Samsung Internet7.0+Opera MobileYes
[Exposed=Window]
interface TaskAttributionTiming : PerformanceEntry {
    readonly attribute DOMString containerType;
    readonly attribute DOMString containerSrc;
    readonly attribute DOMString containerId;
    readonly attribute DOMString containerName;
    [Default] object toJSON();
};

The values of the attributes of a TaskAttributionTiming are set in the processing model in § 4.1 Report long tasks. The following provides an informative summary of how they will be set.

The name attribute’s getter will always return "unknown".

The entryType attribute’s getter will always return "taskattribution".

The startTime attribute’s getter will always return 0.

The duration attribute’s getter will always return 0.

TaskAttributionTiming/containerType

In only one current engine.

FirefoxNoneSafari?Chrome58+
OperaYesEdge79+
Edge (Legacy)NoneIENone
Firefox for AndroidNoneiOS Safari?Chrome for Android58+Android WebView58+Samsung Internet7.0+Opera MobileYes

The containerType attribute’s getter will return the type of the culprit browsing context container, such as "iframe", "embed", or "object". If no single culprit browsing context container is found, it will return "window".

TaskAttributionTiming/containerName

In only one current engine.

FirefoxNoneSafari?Chrome58+
OperaYesEdge79+
Edge (Legacy)NoneIENone
Firefox for AndroidNoneiOS Safari?Chrome for Android58+Android WebView58+Samsung Internet7.0+Opera MobileYes

The containerName attribute’s getter will return the value of the container’s name content attribute. If no single culprit browsing context container is found, it will return the empty string.

TaskAttributionTiming/containerId

In only one current engine.

FirefoxNoneSafari?Chrome58+
OperaYesEdge79+
Edge (Legacy)NoneIENone
Firefox for AndroidNoneiOS Safari?Chrome for Android58+Android WebView58+Samsung Internet7.0+Opera MobileYes

The containerId attribute’s getter will return the value of the container’s id content attribute. If no single culprit browsing context container is found, it will return the empty string.

TaskAttributionTiming/containerSrc

In only one current engine.

FirefoxNoneSafari?Chrome58+
OperaYesEdge79+
Edge (Legacy)NoneIENone
Firefox for AndroidNoneiOS Safari?Chrome for Android58+Android WebView58+Samsung Internet7.0+Opera MobileYes

The containerSrc attribute’s getter will return the value of the container’s src content attribute. If no single culprit browsing context container is found, it will return the empty string.

3.3. Pointing to the culprit

This section is non-normative.

A long task can involve different types of work (such as script, layout, style etc), and it could be executed within different browsing contexts, or it could be global in nature such as a long garbage collection that spans the entire agent cluster or unit of related browsing contexts.

Thus attribution has a couple of facets:

Therefore, name and attribution fields on PerformanceLongTaskTiming together paint the picture for where the blame rests for a long task. When delivering this information the Web’s same-origin policy must be adhered to.

These fields are not independent. The following gives an overview of how they are related:

name Culprit browsing context container implicated by attribution
"self" empty
"same-origin-ancestor" same-origin culprit
"same-origin-descendant" same-origin culprit
"same-origin" same-origin culprit
"cross-origin-ancestor" empty
"cross-origin-descendant" empty
"cross-origin-unreachable" empty
"multiple-contexts" empty
"unknown" empty

4. Processing model

Note: A user agent implementing the Long Tasks API would need to include "longtask" in supportedEntryTypes for Window contexts. This allows developers to detect support for long tasks.

4.1. Report long tasks

Given start time, end time, top-level browsing contexts, and task, perform the following algorithm:
  1. If end time minus start time is less than the long tasks threshold of 50 ms, abort these steps.

  2. Let destinationRealms be an empty set.

  3. Determine the set of JavaScript Realms to which reports will be delivered:

    For each top-level browsing context topmostBC in top-level browsing contexts:

    1. Add topmostBC’s active document's relevant Realm to destinationRealms.

    2. Let descendantBCs be topmostBC’s active document's list of the descendant browsing contexts.

    3. For each descendantBC in descendantBCs, add descendantBC’s active document's relevant Realm to destinationRealms.

  4. A user agent may remove some JavaScript Realms from destinationRealms.

Note: this removal could be used to avoid reporting long tasks for JavaScript Realms that the user agent handles in a separate process. However, this concept is not specified precisely.

there is some ongoing discussion regarding the scope of which Documents gain visibility over which long tasks, so this logic could change in the future. <https://github.com/w3c/longtasks/issues/75>

  1. For each destinationRealm in destinationRealms:

    1. Let name be the empty string. This will be used to report minimal culprit attribution, below.

    2. Let culpritSettings be null.

    3. Process task’s script evaluation environment settings object set to determine name and culpritSettings as follows:

      1. If task’s script evaluation environment settings object set is empty: set name to "unknown" and culpritSettings to null.

      2. Otherwise, if task’s script evaluation environment settings object set's length is greater than one: set name to "multiple-contexts" and culpritSettings to null.

      3. Otherwise, i.e. if task’s script evaluation environment settings object set's length is one:

        1. Set culpritSettings to the single item in task’s script evaluation environment settings object set.

        2. Let destinationSettings be destinationRealm’s settings object.

        3. Let destinationOrigin be destinationSettings’s origin.

        4. Let destinationBC be destinationSettings’s global object's browsing context.

        5. Let culpritBC be culpritSettings’s global object's browsing context.

        6. Assert: culpritBC is not null.

        7. If culpritSettings is the same as destinationSettings, set name to "self".

        8. Otherwise, if culpritSettings’s origin and destinationOrigin are same origin:

          1. If destinationBC is null, set name to "same-origin".

          2. Otherwise, if culpritBC is an ancestor of destinationBC, set name to "same-origin-ancestor".

          3. Otherwise, if destinationBC is an ancestor of culpritBC, set name to "same-origin-descendant".

          4. Otherwise, set name to "same-origin".

        9. Otherwise:

          1. If destinationBC is null, set name to "cross-origin-unreachable".

          2. Otherwise, if culpritBC is an ancestor of destinationBC, set name to "cross-origin-ancestor" and set culpritSettings to null.

            NOTE: this is not reported because of security. Developers should look this up themselves.

          3. Otherwise, if destinationBC is an ancestor of culpritBC, set name to "cross-origin-descendant".

          4. Otherwise, set name to "cross-origin-unreachable".

    4. Let attribution be a new TaskAttributionTiming object with destinationRealm and set its attributes as follows:

      1. Set attribution’s name attribute to "unknown".

        NOTE: future iterations of this API will add more values to the name attribute of a TaskAttributionTiming object, but for now it can only be a single value.

      2. Set attribution’s entryType attribute to "taskattribution".

      3. Set attribution’s startTime and duration to 0.

      4. Set attribution’s containerType attribute to "window".

      5. Set attribution’s containerName and containerSrc attributes to the empty string.

      6. If culpritSettings is not null:

        1. Let culpritBC be culpritSettings’s global object's browsing context.

        2. Assert: culpritBC is not null.

        3. Let container be culpritBC’s browsing context container.

        4. Assert: container is not null.

        5. Set attribution’s containerId attribute to the value of container’s ID, or the empty string if the ID is unset.

        6. If container is an iframe element:

          1. Set attribution’s containerType attribute to "iframe".

          2. Set attribution’s containerName attribute to the value of container’s name content attribute, or the empty string if the attribute is absent.

          3. Set attribution’s containerSrc attribute to the value of container’s src content attribute, or the empty string if the attribute is absent.

          NOTE: it is intentional that we record the frame’s src attribute here, and not its current URL, as this is meant primarily to help identify frames, and allowing discovery of the current URL of a cross-origin iframe is a security problem.

        7. If container is a frame element:

          1. Set attribution’s containerType attribute to "frame".

          2. Set attribution’s containerName attribute to the value of container’s name content attribute, or the empty string if the attribute is absent.

          3. Set attribution’s containerSrc attribute to the value of container’s src content attribute, or the empty string if the attribute is absent.

        8. If container is an object element:

          1. Set attribution’s containerType attribute to "object".

          2. Set attribution’s containerName attribute to the value of container’s name content attribute, or the empty string if the attribute is absent.

          3. Set attribution’s containerSrc attribute to the value of container’s data content attribute, or the empty string if the attribute is absent.

        9. If container is an embed element:

          1. Set attribution’s containerType attribute to "embed".

          2. Set attribution’s containerName attribute to the empty string.

          3. Set attribution’s containerSrc attribute to the value of container’s src content attribute, or the empty string if the attribute is absent.

    5. Create a new PerformanceLongTaskTiming object newEntry with destinationRealm and set its attributes as follows:

      1. Set newEntry’s name attribute to name.

      2. Set newEntry’s entryType attribute to "longtask".

      3. Set newEntry’s startTime attribute to start time.

      4. Set newEntry’s duration attribute to end time minus start time.

      5. If attribution is not null, set newEntry’s attribution attribute to a new frozen array containing the single value attribution.

        NOTE: future iterations of this API will add more values to the attribution attribute, but for now it only contains a single value.

    6. Queue the PerformanceEntry newEntry.

5. Security & privacy considerations

Long Tasks API adheres to the same-origin policy by including origin-safe attribution information about the source of the long task. There is a 50ms threshold for long tasks. Together this provides adequate protection against cross-origin leaks.

The Long Tasks API provides timing information about the duration and type of tasks executed by the user, as well as attribution such as the browsing context causing the function calls. This could enable an attacker to perform side-channel timing attacks to guess the user’s action, or identify the user. For example, a pattern of long script followed by a long render could be put together to guess user’s interaction with a social widget. Detailed function call attribution would be used to determine the user’s action.

While the API doesn’t introduce any new privacy attacks, it could make existing privacy attacks faster. Mitigations for this are possible and can be implemented as needed:

5.1. What is Exposed to Observers?

All observers within the top level page (i.e. all iframes in the page and the main frame) will receive notifications about presence of long tasks. We expose the start time of the task, its duration, and a pointer to the culprit frame. This information can already be observed today, and with higher resolution, using setTimeout. An attacker can do this by clearing everything else on the page and adding the vulnerable cross-origin resource to ensure that delays from the setTimeout are caused by that resource. Observers in other different pages (tabs or windows) should not receive notifications, regardless of the architecture of the user agent.

Cross origin rules for what is exposed:

5.2. Attack Scenarios Considered

The following are the timing attacks considered:

  1. Traditional timing attacks: using external resource load time to reveal the size of private data. For instance the number of hidden pictures in a gallery, whether username is valid, etc. See an example.

  2. Side-channel timing attacks: using time for video parsing, script parsing, App Cache reads or Cache API (service workers) usage to uniquely identify a user, or to create a profile of the user’s age, gender, location, and interests etc. For instance, status updates from a social network can be limited to certain demographic (eg. females of age 20-30) the file size of the permalink page can be used to determine whether the user is in the target demographic.

These scenarios are addressed by the 50ms threshold AND respecting cross-origin boundary i.e. not showing task type or additional attribution to untrusted cross origin observers.

Index

Terms defined by this specification

Terms defined by reference

References

Normative References

[DOM]
Anne van Kesteren. DOM Standard. Living Standard. URL: https://dom.spec.whatwg.org/
[ECMASCRIPT]
ECMAScript Language Specification. URL: https://tc39.es/ecma262/
[HR-TIME-2]
Ilya Grigorik. High Resolution Time Level 2. 21 November 2019. REC. URL: https://www.w3.org/TR/hr-time-2/
[HTML]
Anne van Kesteren; et al. HTML Standard. Living Standard. URL: https://html.spec.whatwg.org/multipage/
[PERFORMANCE-TIMELINE-2]
Ilya Grigorik. Performance Timeline Level 2. 24 October 2019. WD. URL: https://www.w3.org/TR/performance-timeline-2/
[WebIDL]
Boris Zbarsky. Web IDL. 15 December 2016. ED. URL: https://heycam.github.io/webidl/

IDL Index

[Exposed=Window]
interface PerformanceLongTaskTiming : PerformanceEntry {
    readonly attribute FrozenArray<TaskAttributionTiming> attribution;
    [Default] object toJSON();
};

[Exposed=Window]
interface TaskAttributionTiming : PerformanceEntry {
    readonly attribute DOMString containerType;
    readonly attribute DOMString containerSrc;
    readonly attribute DOMString containerId;
    readonly attribute DOMString containerName;
    [Default] object toJSON();
};

Issues Index

there is some ongoing discussion regarding the scope of which Documents gain visibility over which long tasks, so this logic could change in the future. <https://github.com/w3c/longtasks/issues/75>