What are web workers?
11 March 2022 (Updated 11 March 2022)
What is a web worker?
A web worker is like a background thread that lets you run JavaScript code in the background without blocking the main thread.
Workers can improve performance by allowing you to divide computationally expensive tasks among multiple workers, which can then perform those tasks on multi-processor cores.
Create a worker
Send messages to a worker via postMessage
Handle incoming messages inside a worker via onmessage
Terminate a worker
Example usage of a worker
As a very trivial example, we could use a web worker to perform some complex arithmetic and then return the result back to the main page via the postMessage
API when the computation has finished.
Tagged:
JavaScript
Thanks for your comment . Once it's approved, it will appear here.
Leave a comment