Efficient Script Yielding

Editor's Draft July 28th, 2011

This version:
http://dvcs.w3.org/hg/webperf/raw-file/tip/specs/setImmediate/Overview.html
Latest version:
http://dvcs.w3.org/hg/webperf/raw-file/tip/specs/setImmediate/Overview.html
Latest Editor's Draft:
http://dvcs.w3.org/hg/webperf/raw-file/tip/specs/setImmediate/Overview.html
Editors:
Jatinder Mann (Microsoft Corp.) >
Jason Weber (Microsoft Corp.) >

Abstract

This specification defines an interface for web applications to flush the browser event queue and receive an immediate callback.

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 http://www.w3.org/TR/.

This is a work in progress and may change without any notices.

Please send comments to public-web-perf@w3.org (archived) with [ScriptYielding] at the start of the subject line.

This document is produced by the Web Performance Working Group. The Web Performance Working Group is part of the Rich Web Clients Activity in the W3C Interaction Domain.

Publication as a Working 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 5 February 2004 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.

Table of Contents

  1. 1 Introduction
  2. 2 Conformance requirements
  3. 3 Terminology
  4. 4 Efficient Script Yielding
    1. 4.1 Introduction
    2. 4.2 The WindowTimersExtension interface
  5. 5 Processing Model
  6. 6 References
  7. Acknowledgements

1 Introduction

This section is non-normative.

The Efficient Script Yielding specification defines a means for site developers to yield control flow to the user agent before running script. Web developers may want to use this interface to both allow user agent events and display updates to happen before continuing script execution and to avoid long running script dialogs in certain user agents. Many web pages use the setTimeout method with a specified period of zero to attempt to do the same thing. However, setTimeout enforces a minimum delay, even with a specified period of zero, that isn't uniformly implemented across user agents. Removing this minimum delay from setTimeout runs the risk of causing existing webpages, that have come to rely on the minimum delay, to break by going into a seemingly hung state while also significantly increasing the power consumption of the browser.

This specification defines a new method, setImmediate, which will run a callback function immediately after the user agent events and display updates have occurred. This interface will not enforce a minimum delay and will attempt to run the callback as soon as it can.

For example, the following Javascript shows a theoretical web based email client attempting to do a spellcheck operation after the user agent events and display updates have been flushed, to ensure that the site remains responsive:

<!DOCTYPE html>
<html>
 <head>
  <script>
   function emailClient() {
       // Do email work
              
       // When UI is free, check for spelling
       setTimeout(checkSpelling, 0);
   }

   function checkSpelling() { 
       // Scan email for spelling errors 
   }

  </script>
 </head>
 <body onload="emailClient()">
 </body>
</html>

The script callback will always be subject to a minimum delay that may not be uniformly defined across user agents. This results in script that is not running in the most performant manner.

Using the setImmediate method, the page will be able to efficiently yield control flow without minimum delays.

For example, the following Javascript shows a theoretical web based email client attempting to do a spellcheck operation with the setImmediate method to efficiently yield control flow without minimum delays:

<!DOCTYPE html>
<html>
 <head>
  <script>
   function emailClient() {
       // Do email work
            
       // When UI is free, check for spelling
       setImmediate(checkSpelling);
   }

   function checkSpelling() { 
       // Scan email for spelling errors 
   }

  </script>
 </head>
 <body onload="emailClient()">
 </body>
</html>

2 Conformance requirements

All diagrams, examples, and notes in this specification are non-normative, as are all sections explicitly marked non-normative. Everything else in this specification is normative.

The key words "MUST", "MUST NOT", "REQUIRED", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in the normative parts of this document are to be interpreted as described in RFC 2119. For readability, these words do not appear in all uppercase letters in this specification.

Requirements phrased in the imperative as part of algorithms (such as "strip any leading space characters" or "return false and abort these steps") are to be interpreted with the meaning of the key word ("must", "should", "may", etc) used in introducing the algorithm.

Some conformance requirements are phrased as requirements on attributes, methods or objects. Such requirements are to be interpreted as requirements on user agents.

Conformance requirements phrased as algorithms or specific steps may be implemented in any manner, so long as the end result is equivalent. (In particular, the algorithms defined in this specification are intended to be easy to follow, and not intended to be performant.)

3 Terminology

The construction "a Foo object", where Foo is actually an interface, is sometimes used instead of the more accurate "an object implementing the interface Foo".

The term DOM is used to refer to the API set made available to scripts in Web applications, and does not necessarily imply the existence of an actual Document object or of any other Node objects as defined in the DOM Core specifications.

A DOM attribute is said to be getting when its value is being retrieved (such as by author script), and is said to be setting when a new value is assigned to it.

The term "JavaScript" is used to refer to ECMA-262, rather than the official term ECMAScript, since the term JavaScript is more widely known.

4 Efficient Script Yielding

4.1 Introduction

This section is non-normative

This specification introduces an interface that allows web applications to efficiently yield script control without subjecting running the callback to minimum delays.

4.2 The WindowTimersExtension interface

[NoInterfaceObject]
interface WindowTimersExtension {
  long setImmediate(in any handler, in optional any... args);
  void clearImmediate(in long handle);
};
Window implements WindowTimersExtension;
window . setImmediate( handler [, arguments ] )

Schedules to run handler immediately after user agent events have been flushed. Any arguments are passed straight through to the handler.

window . clearImmediate( handle )

Cancels the immediate set with setImmediate() identified by handle.

This API does not guarantee that the callback will be run immediately. Delays due to CPU load, other tasks, etc, may occur.

Each object that implements the WindowTimersExtension interface has a list of active immediates. Each entry in these lists is identified by a number, which must be unique within its list for the lifetime of the object that implements the WindowTimersExtention interface.

5 Processing Model

setImmediate method must run the following steps:

  1. Let handle be a user-agent-defined integer that is greater than zero that will identify the immediate to be set by this call.

  2. Add an entry to the list of active immediates for handle.

  3. Get the handle set by this setImmediate() call from the list of active immediates, and let task be the result.

  4. Continue running this algorithm asynchronously.

  5. Wait until the Document is fully active.

  6. Wait until any invocations of this algorithm started before this one have completed.

  7. Queue the task task.

When the above method is invoked and tries to get immediate in list list, it must run the following steps:

  1. If the first argument to the invoked method is an object that has an internal [[Call]] method, then return a task that checks if the entry for handle in list has been cleared, and if it has not, calls the aforementioned [[Call]] method with as its arguments the second and subsequent arguments to the invoked method (if any) and abort these steps. [ECMA262]

    Otherwise, continue with the remaining steps.

  2. Apply the ToString() abstract operation to the first argument to the method, and let script source be the result. [ECMA262]

  3. Let script language be JavaScript.

  4. Return a task that checks if the entry for handle in list has been cleared, and if it has not, creates a script using script source as the script source, scripting language as the scripting language.

6 References

[ECMA-262]
ECMAScript Language Specification, 5th Edition. ECMA International, Standard ECMA-262, December 2009. This version of the ECMAScript Language is available from http://www.ecma-international.org/publications/standards/Ecma-262.htm.
[HTML5]
HTML5, Ian Hickson, Editor. World Wide Web Consortium, May 2011. This version of the HTML5 is available from http://www.w3.org/TR/html5/. The latest editor's draft is available at http://dev.w3.org/html5/spec/.

Acknowledgements