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, these events are then dequeued one-by-one by the browser and executed.
However, some task can take a long time (multiple frames), and if and when that happens, the UI thread is locked and all other tasks are blocked 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 long tasks often tie up the main thread and prevent the user from interactive with the page even though the page is visually rendered. Poorly designed third-party content is a frequent culprit.
- High/variable input latency:
-
critical user interaction events (tap, click, scroll, wheel, etc) are queued behind long tasks, which yields janky and unpredictable user experience.
- High/variable event handling latency:
-
similar to input, but for processing event callbacks (e.g. onload events, and so on), which delay application updates.
- Janky animations and scrolling:
-
some animation and scrolling interactions require coordination between compositor and main threads; if the main thread is blocked due to a long task, 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 calls: 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 timer. This mostly works, but it has several bad performance implications: the application is polling to detect long tasks, which prevents quiescence and long idle blocks (see requestIdleCallback); it’s bad for battery life; there is no way to know who caused the delay (e.g. first party vs third party code)
RAIL performance model suggests that applications should respond in under 100ms to user input; for touch move and scrolling in under 16ms. Our goal with this API is to surface notifications about tasks that may prevent the application from hitting these targets.
1.1. Usage Example
var observer = new PerformanceObserver(function(list) { var perfEntries = list.getEntries(); for (var i = 0; i < perfEntries.length; i++) { // Process long task notifications: // report back for analytics and monitoring // ... } }); // register observer for long task notifications observer.observe({entryTypes: ["longtask"]}); // Long script execution after this will result in queueing // and receiving “longtask” entries in the observer.
2. Terminology
Long task refers to an event loop task that exceeds 50ms.
Frame or frame context refers to the browsing context, such as iframe (not animation frame), embed or object in which some work (such as script or layout) occurs.
Culprit frame refers to the browsing context (iframe, embed or 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 AND which browsing context is responsible for that work.
3. Long Task Timing
Long Task timing involves the following new interfaces
3.1. PerformanceLongTaskTiming
interface
interfacePerformanceLongTaskTiming
: PerformanceEntry { readonly attribute FrozenArray<TaskAttributionTiming>attribution
; };
PerformanceLongTaskTiming
extends the following attributes of PerformanceEntry
interface:
-
The
name
attribute must returnDOMString
for minimal frame attribution. Possible values of name are:-
self
: long task is from within my own frame -
same-origin-ancestor
: long task is from a same-origin ancestor frame -
same-origin-descendant
: long task is from a same-origin descendant frame -
same-origin
: long task is from an unreachable same-origin frame -
cross-origin-ancestor
: long task is from a cross-origin ancestor frame -
cross-origin-descendant
: long task is from a cross-origin descendant frame -
cross-origin-unreachable
: long task is from a cross-origin unreachable frame -
multiple-contexts
: multiple frame contexts were involved in the long task -
unknown
: none of the above
-
-
The
entryType
attribute must return"longtask"
. -
The
startTime
attribute MUST return aDOMHighResTimeStamp
of when the task started. -
The
duration
attribute MUST return aDOMHighResTimeStamp
equal to the elapsed time between the start and end of task
PerformanceLongTaskTiming
adds the following attributes:
-
The
attribution
field returns a sequence ofTaskAttributionTiming
entries.
3.2. TaskAttributionTiming
interface
interface TaskAttributionTiming
: PerformanceEntry {
readonly attribute DOMString containerType;
readonly attribute DOMString containerSrc;
readonly attribute DOMString containerId;
readonly attribute DOMString containerName;
};
TaskAttributionTiming
extends the following attributes of PerformanceEntry
interface:
-
The
name
attribute must returnDOMString
indicating type of attribution. Currently this can"script"
. -
The
entryType
attribute must returnDOMString
"taskattribution"
-
The
startTime
attribute MUST return 0. -
The
duration
attribute MUST return 0.
TaskAttributionTiming
adds the following attributes:
-
The
containerType
attribute must returnDOMString
with type of frame container, such as"iframe"
,"embed"
,"object"
. -
The
containerName
attribute must returnDOMString
with container’sname
attribute. -
The
containerId
attribute must returnDOMString
with container’sid
attribute. -
The
containerSrc
attribute must returnDOMString
with container’ssrc
attribute.
3.3. Pointing to the culprit
Long task represents the top level event loop task. Within this task, different types of work (such as script, layout, style etc) may be done, and they could be executed within different frame contexts. The type of work could also be global in nature such as a long GC that is process or frame-tree wide.
Thus pointing to the culprit has couple of facets:
-
Pointing to the overall frame to blame for the long task on the whole: this is refered to as "minimal frame attribution" and is captured in the
name
field -
Pointing to the type of work involved in the task, and its associated frame context: this is captured in
TaskAttributionTiming
objects in theattribution
field ofPerformanceLongTaskTiming
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 origin-policy must be adhered to.
As an illustration, the TaskAttributionTiming
entry in attribution
is populated with "script" work, and the container or frame implicated in attribution should match up with the name
as follows:
value of name
| culprit frame implicated in attribution
|
---|---|
self
| empty |
same-origin-ancestor
| same-origin culprit frame |
same-origin-descendant
| same-origin culprit frame |
same-origin
| same-origin culprit frame |
cross-origin-ancestor
| empty |
cross-origin-descendant
| empty |
cross-origin-unreachable
| empty |
multiple-contexts
| empty |
unknown
| empty |
4. Processing Model
4.1. Modifications to other specifications
4.1.1. HTML: event loop definitions
Each task gets an associated start time, end time, and script evaluation environment settings object set.
4.1.2. HTML: event loop processing model
Before Step #3:
-
Set the selected task’s start time to be the value that would be returned by the
Performance
object’snow()
method.
After Step #3:
-
Set the selected task’s end time to the value be the value that would be returned by the
Performance
object’snow()
method. -
Execute the §4.2.1 Report Long Tasks algorithm (below), passing in the selected task.
4.1.3. HTML: calling scripts
In prepare to run script, add a step at the end to add settings to the currently running task’s script evaluation environment settings object set.
4.2. Additions to the Long Task Spec
4.2.1. Report Long Tasks
Given a task task, perform the following algorithm:
-
If end time minus start time is less than the long tasks threshold of 50 ms, abort these steps.
-
Let destinationRealms be an empty set.
-
Determine the set of JavaScript Realms to which reports will be delivered:
For each environment settings object settings in task’s script evaluation environment settings object set:
-
Let topmostBC be settings’s responsible browsing context’s top-level browsing context.
-
Add topmostBC’s Window’s relevant Realm to destinationRealms.
-
Let descendantBCs be topmostBC’s active document’s list of descendant browsing contexts.
-
For each descendantBC in descendantBCs, add descendantBC’s Window’s relevant Realm to destinationRealms.
-
-
For each destinationRealm in destinationRealms:
-
Let name be the empty string. This will be used to report minimal frame attribution, below.
-
Let culpritSettings be null.
-
Process task’s script evaluation environment settings object set to determine name and culpritSettings as follows:
-
If task’s script evaluation environment settings object set is empty: set name to
“unknown”
and culpritSettings tonull
. -
If task’s script evaluation environment settings object set’s length is greater than one: set name to
"multiple-contexts”
and culpritSettings tonull
. -
If task’s script evaluation environment settings object set’s length is one:
-
Set culpritSettings to the single item in task’s script evaluation environment settings object set.
-
Let destinationOrigin be destinationRealm’s relevant settings object’s origin.
-
Let destinationBC be destinationRealm’s relevant settings object’s responsible browsing context.
-
If culpritSettings’s origin and destinationOrigin are [same origin]:
-
If culpritSettings’s responsible browsing context is an ancestor of destinationBC, set name to
“same-origin-ancestor”
. -
If culpritSettings’s responsible browsing context is a descendant of destinationBC, set name to
“same-origin-descendant”
.
-
-
Otherwise:
-
If culpritSettings’s responsible browsing context is an ancestor of destinationBC, set name to
“cross-origin-ancestor”
and set culpritSettings tonull
.NOTE: this is not reported because of security. Developers should look this up themselves.
-
If culpritSettings’s responsible browsing context is a descendant of destinationBC, set name to
“cross-origin-descendant”
.
-
-
-
-
-
Create a new
TaskAttributionTiming
object attribution and set its attributes as follows:-
Set attribution’s name attribute to
"script"
. -
Set attribution’s entryType attribute to
“taskattribution”
-
Set attribution’s startTime and duration to 0.
-
If culpritSettings is not null, and culpritSettings’s responsible browsing context has a browsing context container that is an iframe element, then let iframe be that element, and perform the following steps:
-
Set attribution’s frameName attribute to the value of iframe’s name content attribute, or null if the attribute is absent.
-
Set attribution’s frameSrc attribute to the value of iframe’s src content attribute, or null 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.
-
Set attribution’s frameId attribute to the value of iframe’s id content attribute, or
null
if the attribute is absent.
-
-
-
Create a new
PerformanceLongTaskTiming
object newEntry and set its attributes as follows:-
Set newEntry’s
name
attribute to name. -
Set newEntry’s
entryType
attribute to“longtask”
-
Set newEntry’s
startTime
attribute to start time -
Set newEntry’s
startTime
attribute to end time minus start time -
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.
-
-
Queue the PerformanceEntry newEntry on destinationRealm.
NOTE: the "queue a PerformanceEntry" algorithm will end up doing nothing if no observers are registered. Implementations likely will want to bail out from this algorithm earlier in that case, instead of assembling all the above information only to find out nobody is listening for it.
5. Security & Privacy Considerations
Long Tasks API adheres to cross-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 security attacks against browser.
However, privacy related attacks are possible, while the API doesn’t introduce any new privacy attacks, it could make existing privacy attacks faster. Mitigations for this are possible and discussed in the security review in this document.
6. Acknowledgements
Special thanks to all the contributors for their technical input and suggestions that led to improvements to this specification.