Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 843909 of Web Workers API

  • Revision slug: Web/API/Web_Workers_API
  • Revision title: Web Workers API
  • Revision id: 843909
  • Created:
  • Creator: chrisdavidmills
  • Is current revision? No
  • Comment

Revision Content

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:

  • Shared workers are workers that can be utilized by multiple scripts running in different windows, IFrames, etc., as long as they are in the same domain as the worker. They are a little more complex than dedicated workers — scripts must communicate via an active port. See {{domxref("SharedWorker")}} for more details.
  • ServiceWorkers essentially act as proxy servers that sit between web applications, and the browser and network (when available). They are intended to (amongst other things) enable the creation of effective offline experiences, intercepting network requests and taking appropriate action based on whether the network is available and updated assets reside on the server. They will also allow access to push notifications and background sync APIs.
  • Chrome Workers are a Firefox-only type of worker that you can use if you are developing add-ons and want to use workers in extensions and have access to js-ctypes in your worker. See {{domxref("ChromeWorker")}} for more details. 
  • Audio Workers provide the ability for direct scripted audio processing to be done inside a web worker context.

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[1] 13 {{CompatGeckoDesktop(8)}} 10.0 11.5 6
Passing data using  transferable objects[2] 17 {{ property_prefix("webkit") }}
21
{{CompatGeckoDesktop(18)}} {{CompatNo}} 15 6
Global {{ domxref("window.URL", "URL") }} 10 as webkitURL
23
{{CompatGeckoDesktop(21)}} 11 15 6 as webkitURL
Feature Android Chrome Mobile Firefox Mobile (Gecko) Firefox OS (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 4.4 4 {{CompatGeckoDesktop(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[1] {{CompatNo}} 4 8 1.0.1 {{CompatNo}} {{CompatNo}} {{CompatNo}}
Passing data using  transferable objects[2] {{CompatNo}} {{CompatNo}} 18 1.0.1 {{CompatNo}} {{CompatNo}} {{CompatNo}}

[1] See structured cloning for more details.

[2] See transferable objects for more details.

See also

Revision Source

<p>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.</p>

<h2 id="Web_Workers_concepts_and_usage">Web Workers concepts and usage</h2>

<p>A worker is an object created using <code>a</code> 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")}}).</p>

<p>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 <code>window</code>, including <a href="/en-US/docs/WebSockets">WebSockets</a>, and data storage mechanisms like <a href="/en-US/docs/Web/API/IndexedDB_API">IndexedDB</a> and the Firefox OS-only <a href="/en-US/docs/Web/API/Data_Store_API">Data Store API</a>.&nbsp; See <a href="/en-US/docs/Web/API/Worker/Functions_and_classes_available_to_workers">Functions and classes available to workers</a> for more details.</p>

<p>Data is sent between workers and the main thread via a system of messages — both sides send their messages using the <code>postMessage()</code> method, and respond to messages via the <code>onmessage</code> event handler (the message is contained within the {{event("Message")}} event's data attribute.) The data is copied rather than shared.</p>

<p>Workers may in turn spawn new workers, as long as those workers are hosted within the same origin as the parent page.&nbsp; In addition, workers may use <a class="internal" href="/en/DOM/XMLHttpRequest" title="En/XMLHttpRequest"><code>XMLHttpRequest</code></a> for network I/O, with the exception that the <code>responseXML</code> and <code>channel</code> attributes on <code>XMLHttpRequest</code> always return <code>null</code>.</p>

<p>In addition to dedicated workers, there are other types of worker:</p>

<ul>
 <li>Shared workers are workers that can be utilized by multiple scripts running in different windows, IFrames, etc., as long as they are in the same domain as the worker. They are a little more complex than dedicated workers — scripts must communicate via an active port. See {{domxref("SharedWorker")}} for more details.</li>
 <li><a href="/en-US/docs/Web/API/ServiceWorker_API">ServiceWorkers</a> essentially act as proxy servers that sit between web applications, and the browser and network (when available). They are intended to (amongst other things) enable the creation of effective offline experiences, intercepting network requests and taking appropriate action based on whether the network is available and updated assets reside on the server. They will also allow access to push notifications and background sync APIs.</li>
 <li>Chrome Workers are a Firefox-only type of worker that you can use if you are developing add-ons and want to use workers in extensions and have access to <a href="https://developer.mozilla.org/en/js-ctypes" title="en/js-ctypes">js-ctypes</a> in your worker. See {{domxref("ChromeWorker")}} for more details.&nbsp;</li>
 <li><a href="/en-US/docs/Web/API/Web_Audio_API#Audio_Workers">Audio Workers</a> provide the ability for direct scripted audio processing to be done inside a web worker context.</li>
</ul>

<h2 id="Web_Worker_interfaces">Web Worker interfaces</h2>

<dl>
 <dt>{{domxref("AbstractWorker")}}</dt>
 <dd>Abstracts properties and methods common to all kind of workers (i.e. {{domxref("Worker")}} or {{domxref("SharedWorker")}}).</dd>
 <dt>{{domxref("Worker")}}</dt>
 <dd>Represents a running worker thread, allowing you to pass messages to the running worker code.</dd>
 <dt>{{domxref("SharedWorker")}}</dt>
 <dd>Represents a specific kind of worker that can be <em>accessed</em> from several browsing contexts, being several windows, iframes or even workers.</dd>
 <dt>{{domxref("WorkerGlobalScope")}}</dt>
 <dd>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.</dd>
 <dt>{{domxref("DedicatedWorkerGlobalScope")}}</dt>
 <dd>Represents the scope of a dedicated worker, inheriting from {{domxref("WorkerGlobalScope")}} and adding some dedicated features.</dd>
 <dt>{{domxref("SharedWorkerGlobalScope")}}</dt>
 <dd>Represents the scope of a shared worker, inheriting from {{domxref("WorkerGlobalScope")}} and adding some dedicated features.</dd>
 <dt>{{domxref("WorkerNavigator")}}</dt>
 <dd>Represents the identity and state of the user agent (the client):</dd>
</dl>

<h2 id="Examples">Examples</h2>

<p>We have created a couple of simple demos to show basic usage:</p>

<ul>
 <li><a href="https://github.com/mdn/simple-web-worker">Basic dedicated worker example</a> (<a href="https://mdn.github.io/simple-web-worker/">run dedicated worker</a>).</li>
 <li><a href="https://github.com/mdn/simple-shared-worker">Basic shared worker example</a> (<a href="https://mdn.github.io/simple-shared-worker/">run shared worker</a>).</li>
</ul>

<p>You can find out more information on how these demos work in <a href="/en-US/docs/Web/API/Web_Workers_API/Using_web_workers">Using web workers</a>.</p>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', '#toc-workers')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td>No change from {{SpecName("Web Workers")}}.</td>
  </tr>
  <tr>
   <td>{{SpecName('Web Workers')}}</td>
   <td>{{Spec2('Web Workers')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>

<div>{{CompatibilityTable}}</div>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>4</td>
   <td>{{CompatGeckoDesktop(1.9.1)}}</td>
   <td>10.0</td>
   <td>10.6</td>
   <td>4</td>
  </tr>
  <tr>
   <td>Shared workers</td>
   <td>4</td>
   <td>{{CompatGeckoDesktop(29)}}</td>
   <td>{{CompatNo}}</td>
   <td>10.6</td>
   <td>4</td>
  </tr>
  <tr>
   <td>Passing data using structured cloning[1]</td>
   <td>13</td>
   <td>{{CompatGeckoDesktop(8)}}</td>
   <td>10.0</td>
   <td>11.5</td>
   <td>6</td>
  </tr>
  <tr>
   <td>Passing data using&nbsp; transferable objects[2]</td>
   <td>17 {{ property_prefix("webkit") }}<br />
    21</td>
   <td>{{CompatGeckoDesktop(18)}}</td>
   <td>{{CompatNo}}</td>
   <td>15</td>
   <td>6</td>
  </tr>
  <tr>
   <td>Global {{ domxref("window.URL", "URL") }}</td>
   <td>10 as <code>webkitURL</code><br />
    23</td>
   <td>{{CompatGeckoDesktop(21)}}</td>
   <td>11</td>
   <td>15</td>
   <td>6 as <code>webkitURL</code></td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Chrome Mobile</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>Firefox OS (Gecko)</th>
   <th>IE Phone</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>4.4</td>
   <td>4</td>
   <td>{{CompatGeckoDesktop(1.9.1)}}</td>
   <td>1.0.1</td>
   <td>10.0</td>
   <td>11.5</td>
   <td>5.1</td>
  </tr>
  <tr>
   <td>Shared workers</td>
   <td>{{CompatNo}}</td>
   <td>4</td>
   <td>29</td>
   <td>1.4</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>Passing data using structured cloning[1]</td>
   <td>{{CompatNo}}</td>
   <td>4</td>
   <td>8</td>
   <td>1.0.1</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
  <tr>
   <td>Passing data using&nbsp; transferable objects[2]</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>18</td>
   <td>1.0.1</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
   <td>{{CompatNo}}</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] See <a href="/en/DOM/The_structured_clone_algorithm" title="The structured clone algorithm">structured cloning</a> for more details.</p>

<p>[2] See <a class="external" href="https://www.w3.org/html/wg/drafts/html/master/infrastructure.html#transferable-objects" title="https://dev.w3.org/html5/spec/common-dom-interfaces.html#transferable-objects">transferable objects</a> for more details.</p>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/Web_Workers_API/basic_usage">Using Web Workers</a></li>
 <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/Worker">Worker Interface</a></li>
 <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/SharedWorker">SharedWorker interface</a></li>
 <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorker_API">ServiceWorker API</a></li>
 <li><a href="/en-US/docs/Web/API/Worker/Functions_and_classes_available_to_workers">Functions and classes available to workers</a></li>
 <li><a href="/en-US/docs/Web/API/Web_Workers_API/Advanced_concepts_and_examples">Advanced concepts and examples</a></li>
 <li><a href="/en-US/docs/Web/API/ChromeWorker">ChromeWorker</a>: for using workers in privileged/chrome code</li>
</ul>
Revert to this revision