A web worker, as defined by the
World Wide Web Consortium
The World Wide Web Consortium (W3C) is the main international standards organization for the World Wide Web. Founded in 1994 and led by Tim Berners-Lee, the consortium is made up of member organizations that maintain full-time staff working t ...
(W3C) and the
Web Hypertext Application Technology Working Group (WHATWG), is a
JavaScript
JavaScript (), often abbreviated as JS, is a programming language that is one of the core technologies of the World Wide Web, alongside HTML and CSS. As of 2022, 98% of Website, websites use JavaScript on the Client (computing), client side ...
script
Script may refer to:
Writing systems
* Script, a distinctive writing system, based on a repertoire of specific elements or symbols, or that repertoire
* Script (styles of handwriting)
** Script typeface, a typeface with characteristics of ha ...
executed from an
HTML
The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
page that runs in the
background, independently of scripts that may also have been executed from the same HTML page.
Web workers are often able to utilize
multi-core
A multi-core processor is a microprocessor on a single integrated circuit with two or more separate processing units, called cores, each of which reads and executes program instructions. The instructions are ordinary CPU instructions (such ...
CPU
A central processing unit (CPU), also called a central processor, main processor or just processor, is the electronic circuitry that executes instructions comprising a computer program. The CPU performs basic arithmetic, logic, controlling, and ...
s more effectively.
The W3C and WHATWG envision web workers as long-running scripts that are not interrupted by scripts that respond to clicks or other user interactions. Keeping such workers from being interrupted by user activities should allow Web pages to remain responsive at the same time as they are running long tasks in the background.
The web worker specification is part of the
HTML
The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
Living Standard.
Overview
As envisioned by WHATWG, web workers are relatively heavy-weight and are not intended to be used in large numbers. They are expected to be long-lived, with a high start-up performance cost, and a high per-instance memory cost.
Web workers run outside the context of an HTML document's scripts. Consequently, while they do not have access to the
DOM, they can facilitate
concurrent execution of JavaScript programs.
Features
Web workers interact with the main document via
message passing
In computer science, message passing is a technique for invoking behavior (i.e., running a program) on a computer. The invoking program sends a message to a process (which may be an actor or object) and relies on that process and its supporti ...
. The following code creates a Worker that will execute the JavaScript in the given file.
var worker = new Worker("worker_script.js");
To send a message to the worker, the
postMessage
method of the worker object is used as shown below.
worker.postMessage("Hello World!");
The
onmessage
property uses an event handler to retrieve information from a worker.
worker.onmessage = function(event)
function doSomething()
worker.terminate();
Once a worker is terminated, it goes out of scope and the variable referencing it becomes undefined; at this point a new worker has to be created if needed.
Example
The simplest use of web workers is for performing a computationally expensive task without interrupting the user interface.
In this example, the main document spawns a web worker to compute
prime numbers
A prime number (or a prime) is a natural number greater than 1 that is not a product of two smaller natural numbers. A natural number greater than 1 that is not prime is called a composite number. For example, 5 is prime because the only ways ...
, and progressively displays the most recently found prime number.
The main page is as follows:
Worker example: One-core computation
The highest prime number discovered so far is:
The
Worker()
constructor call creates a web worker and returns a
worker
object representing that web worker, which is used to communicate with the web worker. That object's
onmessage
event handler allows the code to receive messages from the web worker.
The Web Worker itself is as follows:
var n = 1;
var end_value = 10**7;
search: while (n <= end_value)
To send a message back to the page, the
postMessage()
method is used to post a message when a prime is found.
Support
If the browser supports web workers, a Worker property will be available on the global window object. The Worker property will be undefined if the
browser does not support it.
The following example code checks for web worker support on a browser
function browserSupportsWebWorkers()
Web workers are currently supported by
Chrome
Chrome may refer to:
Materials
* Chrome plating, a process of surfacing with chromium
* Chrome alum, a chemical used in mordanting and photographic film
Computing
* Google Chrome, a web browser developed by Google
** ChromeOS, a Google Chrome- ...
,
Opera
Opera is a form of theatre in which music is a fundamental component and dramatic roles are taken by singers. Such a "work" (the literal translation of the Italian word "opera") is typically a collaboration between a composer and a libre ...
,
Edge
Edge or EDGE may refer to:
Technology Computing
* Edge computing, a network load-balancing system
* Edge device, an entry point to a computer network
* Adobe Edge, a graphical development application
* Microsoft Edge, a web browser developed b ...
,
Internet Explorer
Internet Explorer (formerly Microsoft Internet Explorer and Windows Internet Explorer, commonly abbreviated IE or MSIE) is a series of graphical user interface, graphical web browsers developed by Microsoft which was used in the Microsoft Wind ...
(version 10),
Mozilla
Mozilla (stylized as moz://a) is a free software community founded in 1998 by members of Netscape. The Mozilla community uses, develops, spreads and supports Mozilla products, thereby promoting exclusively free software and open standards, w ...
Firefox
Mozilla Firefox, or simply Firefox, is a free and open-source web browser developed by the Mozilla Foundation and its subsidiary, the Mozilla Corporation. It uses the Gecko rendering engine to display web pages, which implements current and ...
, and
Safari
A safari (; ) is an overland journey to observe wild animals, especially in eastern or southern Africa. The so-called "Big Five" game animals of Africa – lion, leopard, rhinoceros, elephant, and Cape buffalo – particularly form an importa ...
.
["Introducing HTML5", Lawson, B. and Sharp, R., 2011.] Mobile Safari for
iOS has supported web workers since iOS 5. The
Android
Android may refer to:
Science and technology
* Android (robot), a humanoid robot or synthetic organism designed to imitate a human
* Android (operating system), Google's mobile operating system
** Bugdroid, a Google mascot sometimes referred to ...
browser first supported web workers in Android 2.1, but support was removed in Android versions 2.2–4.3 before being restored in Android 4.4.
References
External links
Web Workers – W3CWeb Workers – WHATWGUsing Web Workers– Mozilla Developer Network
{{Use dmy dates, date=March 2018
Web development
Web standards
Web programming