Web Workers makes it possible to run a script operation in a background thread separate from the main execution thread of a web application. The advantage of this is that laborious processing can be performed in a separate thread, allowing the main (usually the UI) thread to run without being blocked/slowed down.
A worker is an object created using a constructor (e.g. Worker()
) that runs a named JavaScript file — this file contains the code that will run in the worker thread; workers run in another global context that is different from the current window
. This context is represented by either a DedicatedWorkerGlobalScope
object (in the case of dedicated workers - workers that are utilized by a single script), or a SharedWorkerGlobalScope
(in the case of shared workers - workers that are shared between multiple scripts).
You can run whatever code you like inside the worker thread, with some exceptions. For example, you can't directly manipulate the DOM from inside a worker, or use some default methods and properties of the window
object. But you can use a large number of items available under window
, including WebSockets, and data storage mechanisms like IndexedDB. See Functions and classes available to workers for more details.
Data is sent between workers and the main thread via a system of messages — both sides send their messages using the postMessage()
method, and respond to messages via the onmessage
event handler (the message is contained within the Message
event's data
property). The data is copied rather than shared.
Workers may in turn spawn new workers, as long as those workers are hosted within the same origin as the parent page. In addition, workers may use XMLHttpRequest
for network I/O, with the exception that the responseXML
and channel
attributes on XMLHttpRequest
always return null
.
In addition to dedicated workers, there are other types of worker:
SharedWorker
for more details.ChromeWorker
for more details. Note: As per the Web workers Spec, worker error events should not bubble (see bug 1188141. This has been implemented in Firefox 42.
AbstractWorker
Worker
or SharedWorker
).Worker
WorkerLocation
Worker
.SharedWorker
WorkerGlobalScope
Window
does for normal web content). Different types of worker have scope objects that inherit from this interface and add more specific features.DedicatedWorkerGlobalScope
WorkerGlobalScope
and adding some dedicated features.SharedWorkerGlobalScope
WorkerGlobalScope
and adding some dedicated features.WorkerNavigator
We have created a couple of simple demos to show basic usage:
You can find out more information on how these demos work in Using web workers.
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'Web Workers' in that specification. | Living Standard | Initial definition. |
We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 4 | (Yes) | 3.5 (1.9.1) | 10 | 10.6 | 4 |
Shared workers | 4 | No support | 29 (29) | No support | 10.6 | 4 |
Passing data using structured cloning | 13 | (Yes) | 8 (8) | 10 | 11.5 | 6 |
Passing data using transferable objects | 17webkit 21 | No support | 18 (18) | No support | 15 | 6 |
Global URL
| 10webkit 32 | (Yes) | 21 (21) | 11 | 15 | 6webkit 7 |
Feature | Android | Chrome for Android | Edge | Firefox Mobile (Gecko) | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | 4.4 | 4 | (Yes) | 1.0 (1.9.1) | 11.5 | 5.1 |
Shared workers | No support | 4 | No support | 29.0 (29) | No support | No support |
Passing data using structured cloning | No support | 4 | (Yes) | 8.0 (8) | No support | No support |
Passing data using transferable objects | No support | No support | No support | 18.0 (18) | No support | No support |
Global URL
| (Yes) | (Yes) | (Yes) | 21.0 (21) | (Yes) | 6webkit 7 |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API