Web worker is a JS working in the background, without affecting the performance of the page.
When executing JS in an HTML page, the page becomes unresponsive until the script is finished.
A web worker is a JavaScript that runs in the background, independently of other scripts, without affecting the performance of the page.
w = new Worker("demo_workers.js"); w.onmessage = function(event) { document.getElementById("result").innerHTML = event.data; }; w.terminate();
Bundlers like Webpack and Vite recommends passing urls resolved to relative to import.meta.url
.
const myWorker = new Worker(new URL("worker.js", import.meta.url));