Skip to content
EntityQ7978628· pop 8· linked from 119 articles

Web Worker

Sign in to save

JavaScript script that runs in the background, independent of other scripts in the same HTML document

Described at

3. 10.3 APIs available to workers 1. 10.3.1 Importing scripts and libraries Web Workers API/Using web workers or fetch (with no such restrictions). Once created, a worker can send messages to the JavaScript code that created it by posting messages to an event handler specified by that code (and vice versa).") This specification defines an API for running scripts in the background independently of any user interface scripts. This allows for long-running scripts that are not interrupted by scripts that respond to clicks or other user interactions, and allows long tasks to be executed without yielding to keep the page responsive. Workers (as these background scripts are called herein) are relatively heavy-weight, and are not intended to be used in large numbers. For example, it would be inappropriate to launch one worker for each pixel of a four megapixel image. The examples below show some appropriate uses of workers. Generally, workers are expected to be long-lived, have a high start-up performance cost, and a high per-instance memory cost. There are a variety of uses that workers can be put to. The following subsections show various examples of this use. The simplest use of workers is for performing a computationally expensive task without interrupting the user interface. In this example, the main document spawns a worker to (naïvely) compute prime numbers, and progressively displays the most recently found prime number. The Worker() constructor call creates a worker and returns a Worker object representing that worker, which is used to communicate with the worker. That object's onmessage event handler allows the code to receive messages from the worker. const url = document.querySelector(" image-url"); const filter = document.querySelector(" filter"); const output = document.querySelector(" output"); This second example extends the first one by changing two things: first, messages are received using addEventListener() instead of an event handler IDL attribute , and second, a message is sent to the worker, causing the worker to send another message in return. Received messages are again displayed in a log. In this example, multiple windows (viewers) can be opened that are all viewing the same map. All the windows share the same map information, with a single worker coordinating all the viewers. Each viewer can move around independently, but if they set any data on the map, all the viewers are updated. Open a new viewer Each viewer opens in a new window. You can have as many viewers as you like, they all view the same data. There are several key things worth noting about the way the viewer is written. Registering event listeners in this way also allows you to unregister specific listeners when you are done with them, as is done with the configure() method in this example. A later version of the API, though, might want to offload all the crypto work onto subworkers. This could be done as follows: Notice how the users of the API don't have to even know that this is happening — the API hasn't changed; the library can delegate to subworkers without changing its API, even though it is accepting data using message channels. Dedicated workers use MessagePort objects behind the scenes, and thus support all the same features, such as sending structured data, transferring binary data, and transferring other ports.

Excerpt from a page describing this subject · 40,000 chars · not written by Vinony

Article · 日本語

Web Worker(ウェブ・ワーカー)は、HTMLページから実行され、同じHTMLページから実行される可能性があるスクリプトからは独立にバックグラウンドで動作する、JavaScriptのスクリプトである。World Wide Web Consortium(W3C)とWeb Hypertext Application Technology Working Group(WHATWG)で仕様が定義されている。web workerはマルチコアのCPUをより効率よく利用できることが多い。 Web Workerは、全てのタスクがシングルスレッドで処理されるJavaScriptの持つ問題を解消するための仕様である。JavaScriptで長時間かかる処理を実行すると、入力や画面の描画処理がブロックされて、アプリケーションが操作不能となってしまうため、処理を分割して何度もsetTimeout等で呼び出すなどの工夫が必要であった。しかし、workerを使用することによって、一部の操作について並行処理(バックグラウンド処理)が可能となる。 W3CとWHATWGは、Web Workerをクリックやユーザー操作などに応答するスクリプトによって中断されない長時間実行するスクリプトとして想定している。workerがユーザーアクティビティで中断されないようにすることで、バックグラウンドで長いタスクを実行すると同時に、Webページの応答性を確保できるようになる。 Web Workerの仕様はHTML Living Standardの一部である。

Abstract from DBpedia / Wikipedia · CC BY-SA

Available in 8 languages

via Wikidata sitelinks · CC0