The WebWorker API provides a simple set of methods for web content to run scripts in background threads. Web workers are executed from a main browser window but run independently and in parallel. Once a script in the main thread spawns a worker, the worker can send messages back by posting them to a defined event handler. Data is serialized, not cloned, as it is passed back and forth. This is important to note because poor design or use of too many workers can actually cause performance issues.
However, you can perform I/O through the
XMLHttpRequest object and even import third-party
scripts, as you will see in [Hack #68].
There are three types of web workers: inline, dedicated, and shared. Browser adoption for shared workers is still very sparse, so most of the following hacks will focus on the simpler, dedicated worker.
In the last hack in this chapter, [Hack #69], you will see how shared web workers allow any number of browser window contexts to communicate with a single worker simultaneously.