Web Workers is a powerful feature of HTML5 that hasn’t received very much attention.
It provides an API that allows you to run JavaScript in a separate thread that doesn’t
interfere with the user interface of your web application. This JavaScript runs in parallel
with the main renderer and any of your user interface scripts on it. This allows long
and “processing-heavy” tasks to be executed without making the page unresponsive.
Like threads in other technologies, Web Workers are relatively heavyweight. You don’t
want to use them in large numbers, as each one consumes significant system resources.
Web Workers are expected to handle long tasks that rely on constrained resources (e.g.,
CPU, network bandwidth, etc.). They have a high startup cost and a high instance of
memory cost.
Because it is a new, evolving standard, different browsers implement the Web Workers
specification in different ways. Although some aspects of the implementation are stabilizing,
I suspect that features like access to IndexedDB will be available soon in most
modern browsers. I hope that with this book and the adoption of modern browsers we
will see more usage of this powerful API.
Web apps would run much better if heavy calculations could be performed in the background, rather than compete with the user interface. With this book, you’ll learn how to use Web Workers to run computationally intensive JavaScript code in a thread parallel to the UI. Yes, multi-threaded programing is complicated, but Web Workers provide a simple API that helps you be productive without the complex algorithms.
If you have an intermediate to advanced understanding of JavaScript— especially event handling and callbacks—you’re ready to tackle Web Workers with the tools in this example-driven guide.
-
Start creating Web Workers and understand what they can and can’t do
-
Determine which browser versions support the API
-
Use dedicated Web Workers for tasks that consume a lot of CPU, such as data parsing
-
Explore use cases for creating inline Workers, such as encapsulating a web app in one page
-
Create a shared Worker to communicate multiple web app instances to the server, and other uses
-
Learn best practices for debugging Web Workers
-
Apply Web Workers within the server-side Node environment