ArticleslgStudy

science

Web worker

Web worker is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Web worker rather than just read about it. In short: A web worker, as defined by the World Wide Web Consortium (W3C) and the Web Hypertext Application Technology Working Group (WHATWG), is a JavaScript script executed from an HTML 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 CPUs more effectively.

Key takeaways

  • Web worker belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Web worker to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Web worker from memory before moving on to harder problems.

Reference excerpt

A web worker, as defined by the World Wide Web Consortium (W3C) and the Web Hypertext Application Technology Working Group (WHATWG), is a JavaScript script executed from an HTML 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 CPUs 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 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. The following code creates a Worker that will execute the JavaScript in the given file.

To send a message to the worker, the postMessage method of the worker object is used as shown below.

The onmessage property uses an event handler to retrieve information from a worker.

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, and progressively displays the most recently found prime number. The main page is as follows:

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:

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

Web workers are currently supported by Chrome, Opera, Edge, Internet Explorer (version 10), Mozilla Firefox, and Safari. Mobile Safari for iOS has supported web workers since iOS 5. The Android 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.

See also Web service worker

References

External links Web Workers – W3C Web Workers – WHATWG Using Web Workers – Mozilla Developer Network

Worked examples

Example 1 — a first encounter with Web worker

Start with the simplest possible case. Write down what Web worker claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Web worker before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Web worker ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Web worker

In research
Web worker appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Web worker in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Web worker is common in secondary-school and first-year university syllabi. It links to neighbouring topics Web development, Web programming, Web standards, so understanding it makes those chapters shorter.
In everyday life
Look for Web worker outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Web worker” →

How to study Web worker in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Web worker means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Web worker out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Web worker in simple terms?

A web worker, as defined by the World Wide Web Consortium (W3C) and the Web Hypertext Application Technology Working Group (WHATWG), is a JavaScript script executed from an HTML page that runs in the background, independently of scripts that may also have been executed from the same HTML page. Web…

Why does Web worker matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Web worker?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Web worker.

Tags

  • Web development
  • Web programming
  • Web standards

Keep exploring