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.
Concepts and usageA 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.
In addition to the standard JavaScript set of functions (such as String
, Array
, Object
, JSON
, etc.), you can run almost any code you like inside a worker thread. There are 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. For information about the code that you can run see supported functions, and supported Web APIs.
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 can make network requests using the fetch()
or XMLHttpRequest
APIs (although note that the responseXML
attribute of XMLHttpRequest
will always be null
).
There are a number of different types of workers:
DedicatedWorkerGlobalScope
object.While Window
is not directly available to workers, many of the same methods are defined in a shared mixin (WindowOrWorkerGlobalScope
), and made available to workers through their own WorkerGlobalScope
-derived contexts:
DedicatedWorkerGlobalScope
for dedicated workersSharedWorkerGlobalScope
for shared workersServiceWorkerGlobalScope
for service workersWorker
Represents a running worker thread, allowing you to pass messages to the running worker code.
WorkerLocation
Defines the absolute location of the script executed by the Worker
.
Represents a specific kind of worker that can be accessed from several browsing contexts (i.e., windows, tabs, or iframes) or even other workers.
WorkerGlobalScope
Represents the generic scope of any worker (doing the same job as Window
does for normal web content). Different types of worker have scope objects that inherit from this interface and add more specific features.
DedicatedWorkerGlobalScope
Represents the scope of a dedicated worker, inheriting from WorkerGlobalScope
and adding some dedicated features.
Represents the scope of a shared worker, inheriting from WorkerGlobalScope
and adding some dedicated features.
WorkerNavigator
Represents the identity and state of the user agent (the client).
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.3