Web workers are javascript running independently of other scripts. They don’t affect the page’s performance. However, they can pass messages to the other scripts and vice-versa.
A simple example (Dedicated Worker)
A worker’s context is represented by either DedicatedWorkerGlobalScope or SharedWorkerGlobalScope object (not window).
Dedicated workers: only accessible from the script that first spawned it
Shared workers: can be accessed from multiple scripts.
Shared Worker
Features available to Workers
Due to their multi-threaded behavior, web workers only have access to a subset of JavaScript’s features:
navigator object
location object (read-only)
XMLHttpRequest
setTimeout()/clearTimeout() and setInterval()/clearInterval()
Application Cache
Importing external scripts using the importScripts() method
Spawning other web workers
Workers do NOT have access to:
DOM (it’s not thread-safe)
window object
document object
parent object
Loading External Scripts
You can load external script files or libraries into a worker. The method takes zero or more strings representing the filenames for the resources to import.
Inline Worker
So where do I use it?
Prefetching and/or caching data for later use
In web editors for syntax highlighting, text formatting and spell check