This specification defines an API for running scripts in the background independently of any user interface scripts.
This specification defines an API for running scripts in the background independently of any user interface scripts.
This allows for long-running scripts that are not interrupted by scripts that respond to clicks or other user interactions, and allows long tasks to be executed without yielding to keep the page responsive.
This allows for long-running scripts that are not interrupted by scripts that respond to clicks or other user interactions, and allows long tasks to be executed without yielding to keep the page responsive.
Workers (as these background scripts are called herein) are relatively heavy-weight, and are not intended to be used in large numbers. For example, it would be inappropriate to launch one worker for each pixel of a four megapixel image. The examples below show some appropriate uses of workers.
Workers (as these background scripts are called herein) are relatively heavy-weight, and are not intended to be used in large numbers. For example, it would be inappropriate to launch one worker for each pixel of a four megapixel image. The examples below show some appropriate uses of workers.
Generally, workers are expected to be long-lived, have a high start-up performance cost, and a high per-instance memory cost.
Generally, workers are expected to be long-lived, have a high start-up performance cost, and a high per-instance memory cost.
1.2 Examples
10.1.2 Examples
This section is non-normative.
This section is non-normative.
There are a variety of uses that workers can be put to. The following subsections show various examples of this use.
There are a variety of uses that workers can be put to. The following subsections show various examples of this use.
1.2.1 A background number-crunching worker
10.1.2.1 A background number-crunching worker
This section is non-normative.
This section is non-normative.
The simplest use of workers is for performing a computationally expensive task without interrupting the user interface.
The simplest use of workers is for performing a computationally expensive task without interrupting the user interface.
In this example, the main document spawns a worker to (naïvely) compute prime numbers, and progressively displays the most recently found prime number.
In this example, the main document spawns a worker to (naïvely) compute prime numbers, and progressively displays the most recently found prime number.
The Worker() constructor call creates a worker and returns a Worker object representing that worker, which is used to communicate with the worker. That object's onmessage event handler allows the code to receive messages from the worker.
The Worker() constructor call creates a worker and returns a Worker object representing that worker, which is used to communicate with the worker. That object's onmessage event handler allows the code to receive messages from the worker.
The worker itself is as follows:
The worker itself is as follows:
var n = 1;
var n = 1;
search: while (true) {
search: while (true) {
n += 1;
n += 1;
for (var i = 2; i <= Math.sqrt(n); i += 1)
for (var i = 2; i <= Math.sqrt(n); i += 1)
if (n % i == 0)
if (n % i == 0)
continue search;
continue search;
// found a prime!
// found a prime!
postMessage(n);
postMessage(n);
}
}
The bulk of this code is simply an unoptimized search for a prime number. The postMessage() method is used to send a message back to the page when a prime is found.
The bulk of this code is simply an unoptimised search for a prime number. The postMessage() method is used to send a message back to the page when a prime is found.
View this example online.
View this example online.
1.2.2 Worker used for background I/O
10.1.2.2 Worker used for background I/O
This section is non-normative.
This section is non-normative.
In this example, the main document uses two workers, one for fetching stock updates at regular intervals, and one for performing search queries that the user requests.
In this example, the main document uses two workers, one for fetching stock updates at regular intervals, and one for performing search queries that the user requests.