--- title: Web Workers API slug: Web/API/Web_Workers_API translation_of: Web/API/Web_Workers_API ---

{{DefaultAPISidebar("Web Workers API")}}

Web Workers are a mechanism by which a script operation can be made to run 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.

Web Workers concepts and usage

A worker is an object created using a constructor (e.g. {{domxref("Worker.Worker", "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 {{domxref("window")}}. This context is represented by a {{domxref("DedicatedWorkerGlobalScope")}} object in the case of dedicated workers (standard workers that are utilized by a single script; shared workers use {{domxref("SharedWorkerGlobalScope")}}).

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 {{domxref("window")}} object. But you can use a large number of items available under window, including WebSockets, and data storage mechanisms like IndexedDB and the Firefox OS-only Data Store API.  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 {{event("Message")}} event's data attribute.) 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:

Note: As per the Web workers Spec, worker error events should not bubble (see {{bug(1188141)}}. This has been implemented in Firefox 42.

Web Worker interfaces

{{domxref("AbstractWorker")}}
Abstracts properties and methods common to all kind of workers (i.e. {{domxref("Worker")}} or {{domxref("SharedWorker")}}).
{{domxref("Worker")}}
Represents a running worker thread, allowing you to pass messages to the running worker code.
{{domxref("SharedWorker")}}
Represents a specific kind of worker that can be accessed from several browsing contexts, being several windows, iframes or even workers.
{{domxref("WorkerGlobalScope")}}
Represents the generic scope of any worker (doing the same job as {{domxref("Window")}} does for normal web content). Different types of worker have scope objects that inherit from this interface and add more specific features.
{{domxref("DedicatedWorkerGlobalScope")}}
Represents the scope of a dedicated worker, inheriting from {{domxref("WorkerGlobalScope")}} and adding some dedicated features.
{{domxref("SharedWorkerGlobalScope")}}
Represents the scope of a shared worker, inheriting from {{domxref("WorkerGlobalScope")}} and adding some dedicated features.
{{domxref("WorkerNavigator")}}
Represents the identity and state of the user agent (the client):

Examples

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.

Specifications

Specification Status Comment
{{SpecName('HTML WHATWG', '#toc-workers')}} {{Spec2('HTML WHATWG')}} No change from {{SpecName("Web Workers")}}.
{{SpecName('Web Workers')}} {{Spec2('Web Workers')}} Initial definition.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 4 {{CompatGeckoDesktop(1.9.1)}} 10.0 10.6 4
Shared workers 4 {{CompatGeckoDesktop(29)}} {{CompatNo}} 10.6 4
Passing data using structured cloning 13 {{CompatGeckoDesktop(8)}} 10.0 11.5 6
Passing data using  transferable objects 17 {{property_prefix("webkit")}}
21
{{CompatGeckoDesktop(18)}} {{CompatNo}} 15 6
Global {{domxref("window.URL", "URL")}} 10[1]
23
{{CompatGeckoDesktop(21)}} 11 15 6[1]
Feature Android Chrome for Android Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 4.4 4 {{CompatGeckoMobile(1.9.1)}} 1.0.1 10.0 11.5 5.1
Shared workers {{CompatNo}} 4 29 1.4 {{CompatNo}} {{CompatNo}} {{CompatNo}}
Passing data using structured cloning {{CompatNo}} 4 8 1.0.1 {{CompatNo}} {{CompatNo}} {{CompatNo}}
Passing data using  transferable objects {{CompatNo}} {{CompatNo}} 18 1.0.1 {{CompatNo}} {{CompatNo}} {{CompatNo}}

[1] As webkitURL.

See also