Dedicated Web Workers: separate computation
Quick answer
A dedicated worker runs JavaScript in a separate worker global environment. Use it for sufficiently expensive computation when keeping the page responsive is important.
How it works
Create a worker with new Worker(url); a module worker uses {type:"module"}. The worker has its own global scope and event loop; it cannot access the page DOM. The page sends data and receives results using messages. Worker creation and communication have costs, so small calculations often belong on the main thread. The examples use temporary blob URLs for local demonstration; production code commonly loads a separate same-origin script and follows CSP.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const url=URL.createObjectURL(new Blob(['self.onmessage=e=>self.postMessage(e.data.reduce((a,b)=>a+b,0));'],{type:"text/javascript"}));let w;try{w=new Worker(url);const result=new Promise((resolve,reject)=>{w.onmessage=e=>resolve(e.data);w.onerror=e=>reject(new Error(e.message));});w.postMessage([2,3,4]);console.log(JSON.stringify(await result));}finally{w?.terminate();URL.revokeObjectURL(url);}})()Expected console output:
9The worker computes the sum and the main context receives it.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const url=URL.createObjectURL(new Blob(['self.onmessage=()=>self.postMessage(typeof document);'],{type:"text/javascript"}));let w;try{w=new Worker(url);const result=new Promise((resolve,reject)=>{w.onmessage=e=>resolve(e.data);w.onerror=e=>reject(new Error(e.message));});w.postMessage(null);console.log(JSON.stringify(await result));}finally{w?.terminate();URL.revokeObjectURL(url);}})()Expected console output:
"undefined"The worker does not expose the page document.
Common mistakes
Do not move DOM calls into the worker or assume a worker makes every operation faster. Terminate a worker that is no longer needed.
Practice
Send a bounded array of numbers to a worker, calculate their sum, and render the returned number on the page.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Dedicated Web Workers: separate computation — Knowledge Check
Apply the concept and reason about local example output.
Which operation should stay on the page in the dedicated-worker pattern?
What is a cost of creating a new worker?
Which Worker option selects a module script?
What is the expected console output of Dedicated Web Workers: separate computation example 1?
(async () => {const url=URL.createObjectURL(new Blob(['self.onmessage=e=>self.postMessage(e.data.reduce((a,b)=>a+b,0));'],{type:"text/javascript"}));let w;try{w=new Worker(url);const result=new Promise((resolve,reject)=>{w.onmessage=e=>resolve(e.data);w.onerror=e=>reject(new Error(e.message));});w.postMessage([2,3,4]);console.log(JSON.stringify(await result));}finally{w?.terminate();URL.revokeObjectURL(url);}})()What is the expected console output of Dedicated Web Workers: separate computation example 2?
(async () => {const url=URL.createObjectURL(new Blob(['self.onmessage=()=>self.postMessage(typeof document);'],{type:"text/javascript"}));let w;try{w=new Worker(url);const result=new Promise((resolve,reject)=>{w.onmessage=e=>resolve(e.data);w.onerror=e=>reject(new Error(e.message));});w.postMessage(null);console.log(JSON.stringify(await result));}finally{w?.terminate();URL.revokeObjectURL(url);}})()