• Stars
    star
    233
  • Rank 172,230 (Top 4 %)
  • Language Bikeshed
  • License
    Other
  • Created over 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Long Task API

Long Task API

Long Tasks is a new real user measurement (RUM) performance API to enable applications to measure responsiveness. It enables detecting 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.

Background

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. the 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 — see “the anatomy of a frame” for a high-level overview of this process in Blink.

However, some tasks 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 interacting 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; and there is no way to know what 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.

Terminology

Major terms:

  • frame or frame context refers to the browsing context, such as iframe (not animation frame), embed or object
  • culprit frame refers to the frame or container (iframe, object, embed etc) that is being implicated for the 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 or frame is responsible for that work.
  • minimal frame attribution refers to the browsing context or frame that is being implicated overall for the long task

V1 API

Long Task API introduces a new PerformanceEntry object, which will report instances of long tasks:

interface PerformanceLongTaskTiming : PerformanceEntry {
  [SameObject, SaveSameObject] readonly attribute FrozenArray<TaskAttributionTiming> attribution;
};

Attribute definitions of PerformanceLongTaskTiming:

  • entryType: "longtask"

  • startTime: DOMHighResTimeStamp of when long task started

  • duration: elapsed time (as DOMHighResTimeStamp) between start and finish of task

  • name: minimal frame attribution, eg. "same-origin", "cross-origin", "unknown" etc. Possible values are:

  • "self"

  • "same-origin-ancestor"

  • "same-origin-descendant"

  • "same-origin"

  • "cross-origin-ancestor"

  • "cross-origin-descendant"

  • "cross-origin-unreachable"

  • "multiple-contexts"

  • "unknown"

  • attribution: sequence of TaskAttributionTiming, a new PerformanceEntry object to report attribution within long tasks. To see how attribute is populated for different values of name see the section below: Pointing to the culprit

interface TaskAttributionTiming : PerformanceEntry {
  readonly attribute DOMString containerType;
  readonly attribute DOMString containerSrc;
  readonly attribute DOMString containerId;
  readonly attribute DOMString containerName;
};

Attribute definitions of TaskAttributionTiming:

  • entryType: “taskattribution”
  • startTime: 0
  • duration: 0
  • name: type of attribution, eg. "script" or "layout"
  • containerType: type of container for culprit frame eg. "iframe" (most common), "embed", "object".
  • containerName: DOMString, container’s name attribute
  • containerId: DOMString, container’s id attribute
  • containerSrc: DOMString, container’s src attribute

Long tasks events will be delivered to all observers (in frames within the page or tab) regardless of which frame was responsible for the long task. The goal is to allow all pages on the web to know if and who (first party content or third party content) is causing disruptions.

The name field provides minimal frame attribution so that the observing frame can respond to the issue in the proper way. In addition, the attribution field provides further insight into the type of work (script, layout etc) that caused the long task as well as which frame is responsible for that work. For more details on how the attribution is set, see the "Pointing to the culprit" section.

The above covers existing use cases found in the wild, enables document-level attribution, and eliminates the negative performance implications mentioned earlier. To receive these notifications, the application can subscribe to them via PerformanceObserver interface:

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 long task notifications.
// Since the "buffered" flag is set, longtasks that already occurred are received.
observer.observe({type: "longtask", buffered: true});

// Long script execution after this will result in queueing 
// and receiving “longtask” entries in the observer.

Long-task threshold is 50ms. That is, the UA should emit long-task events whenever it detects tasks whose execution time exceeds >50ms.

Demo

For a quick demo, visit this website on a browser which supports the Long Tasks API.

For a demo of long tasks from same-origin & cross-origin frames, see this website. Interacting with the iframed wikipedia page will generate cross-origin long task notifications.

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 the attribution field of PerformanceLongTaskTiming

Therefore, name and attribution fields on PerformanceLongTaskTiming together paint the picture for where the blame rests for a long task.

The security model of the web means that sometimes a long task will happen in an iframe that is unreachable from the observing frame. For instance, a long task might happen in a deeply nested iframe that is different from my origin. Or similarly, I might be an iframe doubly embedded in a document, and a long task will happen in the top-level browsing context. In the web security model, I can know from which direction the issue came, one of my ancestors or descendants, but to preserve the frame origin model, we must be careful about pointing to the specific container or frame.

Currently the TaskAttributionTiming entry in attribution is populated with "script" work (in the future layout, style etc will be added). The container or frame implicated in attribution should match up with the name as follows:

value of name 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 first cross-origin child frame between my own frame and culprit frame
cross-origin-unreachable empty
multiple-contexts empty
unknown empty

Privacy & Security

Long Tasks API surfaces long tasks greater than a threshold (50ms) to developers via Javascript (Performance Observer API). It includes 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 expedite existing privacy attacks. If this were to become an concern, additional mitigations can be implemented to address this such as dropping "culprit" after a per-target origin threshold is exceeded, or limiting to 10 origins per minute etc.

Detailed Security & Privacy doc is here: https://docs.google.com/document/d/1tIMI1gau_q6X5EBnjDNiFS5NWV9cpYJ5KKA7xPd3VB8/edit#

V2 API Sketch

See: https://docs.google.com/document/d/125d69JAC7nyx-Ob0a9Z31d1uHUGu4myYQ3os9EnGfdU/edit

Alternatives Considered

Why not just show sub-tasks vs. top-level tasks with attribution?

This API will show toplevel long tasks along with attribution for specific sub-tasks which were problematic. For instance, within a 50ms toplevel task, sub-tasks such as a 20ms script execution or a 30ms style & layout update -- will be attributed. This raises the question -- why show the toplevel task at all? Why not only show long sub-tasks such as script, style & layout etc that are directly actionable by the user? The top level task may contain some un-attributable segments such as browser work eg. GC or browser events etc.

The rationale here is that showing the toplevel task is good for web developers, even though they will actively consume the actionable sub-tasks such as long scripts and act on them. Over time the sub-task attribution will keep expanding, making more of the long task actionable. Showing the top-level task gives developers a direct indication of main thread busy-ness, and since this directly impacts the user experience, it is appropriate for them to know about it as a problem signal -- even if they cannot have complete visibility or full actionability for the entire length of the long task. In many cases the developers may be able to repro in lab or locally and glean additional insights and get to the root cause. Long tasks provide context to long sub-tasks, for instance, a 20ms style and layout or a 25ms script execution may not be terrible by themselves, but if they happen consecutively (eg. script started from rAF) and cause a long 50ms task, then this is a problem for user responsiveness.

More Repositories

1

csswg-drafts

CSS Working Group Editor Drafts
Bikeshed
4,475
star
2

ServiceWorker

Service Workers
HTML
3,629
star
3

IntersectionObserver

Intersection Observer
Bikeshed
3,622
star
4

html

Deliverables of the HTML Working Group until October 2018
HTML
1,973
star
5

css-houdini-drafts

Mirror of https://hg.css-houdini.org/drafts
Bikeshed
1,837
star
6

epubcheck

The conformance checker for EPUB publications
Java
1,640
star
7

aria-practices

WAI-ARIA Authoring Practices Guide (APG)
HTML
1,075
star
8

webauthn

Web Authentication: An API for accessing Public Key Credentials
HTML
1,012
star
9

webcodecs

WebCodecs is a flexible web API for encoding and decoding audio and video.
HTML
990
star
10

wcag

Web Content Accessibility Guidelines
HTML
906
star
11

webappsec-change-password-url

A Well-Known URL for Changing Passwords
Bikeshed
899
star
12

webtransport

WebTransport is a web API for flexible data transport
Bikeshed
835
star
13

clreq

Requirements for Chinese Text Layout
HTML
727
star
14

manifest

Manifest for web apps
HTML
658
star
15

svgwg

SVG Working Group specifications
HTML
656
star
16

webdriver

Remote control interface that enables introspection and control of user agents.
HTML
652
star
17

webappsec

Web Application Security Working Group repo
HTML
603
star
18

trusted-types

A browser API to prevent DOM-Based Cross Site Scripting in modern web applications.
JavaScript
603
star
19

webextensions

Charter and administrivia for the WebExtensions Community Group (WECG)
Bikeshed
591
star
20

aria

Accessible Rich Internet Applications (WAI-ARIA)
HTML
582
star
21

chinese-ig

Web中文兴趣组
HTML
536
star
22

musicxml

MusicXML specification
XSLT
512
star
23

payment-request

Payment Request API
HTML
489
star
24

trace-context

Trace Context
Python
434
star
25

webrtc-pc

WebRTC 1.0 API
HTML
423
star
26

did-core

W3C Decentralized Identifier Specification v1.0
HTML
399
star
27

webappsec-permissions-policy

A mechanism to selectively enable and disable browser features and APIs
Bikeshed
398
star
28

web-performance

W3C Web Performance Working Group repo
HTML
395
star
29

web-advertising

Web Advertising BG - https://www.w3.org/community/web-adv/
HTML
380
star
30

miniapp

MiniApps Standardization
JavaScript
366
star
31

web-share

Web API proposal for sharing data from a web page
HTML
353
star
32

webdriver-bidi

Bidirectional WebDriver protocol for browser automation
Bikeshed
335
star
33

Mobile-Checker

The Mobile Checker is a tool for Web developers who want to make their Web page or Web app work better on mobile devices.
JavaScript
322
star
34

web-nfc

Web NFC
HTML
313
star
35

picture-in-picture

Picture-in-Picture (PiP)
Bikeshed
311
star
36

webref

Machine-readable references of terms defined in web browser specifications
JavaScript
287
star
37

websub

WebSub Spec in Social Web Working Group
HTML
287
star
38

vc-data-model

W3C Verifiable Credentials v2.0 Specification
HTML
286
star
39

wai-tutorials

W3C WAI’s Web Accessibility Tutorials
JavaScript
284
star
40

activitystreams

Activity Streams 2.0
HTML
282
star
41

epub-specs

Shared workspace for EPUB 3 specifications.
HTML
274
star
42

EasierRDF

Making RDF easy enough for most developers
Python
267
star
43

webcrypto

The W3C Web Cryptography API
HTML
265
star
44

danmaku

Bullet Chatting Proposal
HTML
260
star
45

webpayments

The document repo for the Web Payments Working Group
HTML
257
star
46

paint-timing

Paint Timing
Bikeshed
251
star
47

media-source

Media Source Extensions
HTML
245
star
48

IndexedDB

Indexed Database API
Bikeshed
240
star
49

webidl2.js

WebIDL parser
JavaScript
224
star
50

smufl

Standard Music Font Layout
HTML
214
star
51

wot

Web of Things
HTML
213
star
52

browser-specs

A machine-readable list of Web specifications
JavaScript
212
star
53

web-of-things-framework

JavaScript
208
star
54

webappsec-csp

WebAppSec Content Security Policy
HTML
205
star
55

webcomponents-cg

Web Components community group
HTML
202
star
56

css-validator

W3C CSS Validation Service
Java
194
star
57

web-share-target

Web API proposal for receiving shared data
HTML
192
star
58

web-roadmaps

Framework for Web technology roadmaps
HTML
190
star
59

silver

Accessibility Guidelines "Silver"
HTML
190
star
60

process

W3C Process Document
HTML
189
star
61

editing

Specs and explainers maintained by the editing task force
HTML
181
star
62

html-aria

ARIA in HTML
HTML
180
star
63

w3c-api

The W3C API
HTML
178
star
64

encrypted-media

Encrypted Media Extensions
HTML
169
star
65

p2p-webtransport

Interface to create and manage QUIC streams
HTML
164
star
66

csvw

Documents produced by the CSV on the Web Working Group
HTML
162
star
67

sdw

Repository for the Spatial Data on the Web Working Group
HTML
150
star
68

clipboard-apis

Clipboard API and events
HTML
148
star
69

strategy

team-strat, on GitHub, working in public. Current state: DRAFT
148
star
70

uievents

UI Events
HTML
147
star
71

automotive

W3C Automotive Working Group Specifications
HTML
146
star
72

webvtt.js

WebVTT parser and validator
JavaScript
146
star
73

push-api

Push API
HTML
142
star
74

gamepad

Gamepad
HTML
142
star
75

web-annotation

Web Annotation Working Group repository, see README for links to specs
HTML
141
star
76

wcag21

Repository used during WCAG 2.1 development. New issues, Technique ideas, and comments should be filed at the WCAG repository at https://github.com/w3c/wcag.
HTML
140
star
77

elements-of-html

Elements of HTML per version
HTML
137
star
78

mnx

Music Notation CG next-generation music markup proposal.
HTML
136
star
79

libwww

Libwww is a highly modular, general-purpose client side Web API written in C for Unix and Windows (Win32). It's well suited for both small and large applications, like browser/editors, robots, batch tools, etc. Pluggable modules provided with libwww include complete HTTP/1.1 (with caching, pipelining, PUT, POST, Digest Authentication, deflate, etc), MySQL logging, FTP, HTML/4, XML (expat), RDF (SiRPAC), WebDAV, and much more. The purpose of libwww is to serve as a testbed for protocol experiments. This is a complete mirror of the libwww CVS repository
C
135
star
80

w3c.github.io

The W3C organisation
HTML
134
star
81

webrtc-stats

WebRTC Statistics
HTML
128
star
82

aria-at

Assistive Technology ARIA Experience Assessment
HTML
127
star
83

sensors

Generic Sensor API
HTML
127
star
84

ortc

ORTC Community Group specification repository (see W3C WebRTC for official standards track)
HTML
124
star
85

web-locks

Cross-tab resource coordination API
Bikeshed
124
star
86

dxwg

Data Catalog Vocabulary (DCAT)
HTML
124
star
87

sparql-dev

SPARQL dev Community Group
123
star
88

wot-thing-description

Web of Things (WoT) Thing Description
HTML
122
star
89

webrtc-encoded-transform

WebRTC Encoded Transform
Bikeshed
122
star
90

mediacapture-main

Media Capture and Streams specification (aka getUserMedia)
HTML
121
star
91

mediasession

Media Session API
Bikeshed
120
star
92

did-extensions

Decentralized Identifier Ecosystem Extensions
HTML
120
star
93

rdf-star

RDF-star specification
HTML
120
star
94

link-checker

Check links and anchors in Web pages or full Web sites.
Perl
119
star
95

Unicorn

Unicorn - W3C's Unified Validator
Java
118
star
96

markup-validator

HTML
117
star
97

resource-timing

Resource Timing
HTML
116
star
98

web-animations

🚫 Old repository for the Web Animations specification 🚫. Updated repository:
HTML
115
star
99

csswg-test

The former CSS WG test repository
114
star
100

secure-payment-confirmation

Secure Payment Confirmation (SPC)
Bikeshed
113
star