In Angular, the worker URL must use new URL('./compute.worker', import.meta.url) for correct bundling.
Step 2: Identify the missing part
The code uses a plain string './compute.worker' which causes a runtime error because the path is unresolved.
Final Answer:
Missing import.meta.url in Worker URL -> Option A
Quick Check:
Worker URL needs import.meta.url [OK]
Hint: Always use new URL(path, import.meta.url) for worker paths [OK]
Common Mistakes:
Calling postMessage before setting onmessage
Using absolute URL instead of relative with import.meta.url
Making onmessage async unnecessarily
5. You want to perform a heavy calculation on a large array without freezing the UI. Which approach best uses Angular Web Workers to achieve this?
1. Create a worker with Angular CLI. 2. Send the large array to the worker using postMessage. 3. In the worker, process the array and send back the result. 4. Update the UI with the result when received.
What is the best practice to handle the large data transfer efficiently?
hard
A. Use Transferable Objects like ArrayBuffer to avoid copying data
B. Send data as JSON strings to simplify parsing
C. Split the array into small chunks and send each separately
D. Process the array on the main thread to avoid messaging overhead
Solution
Step 1: Understand data transfer in Web Workers
Sending large data copies it by default, which can be slow and freeze UI.
Step 2: Use Transferable Objects to optimize
Transferable Objects like ArrayBuffer transfer ownership without copying, making communication efficient.
Step 3: Compare options
Sending JSON strings adds parsing overhead, splitting chunks adds complexity, and processing on main thread blocks UI.
Final Answer:
Use Transferable Objects like ArrayBuffer to avoid copying data -> Option A
Quick Check:
Transferable Objects = efficient large data transfer [OK]
Hint: Transfer large data with Transferable Objects to avoid UI freeze [OK]
Common Mistakes:
Sending large data as JSON causing slow parsing
Splitting data unnecessarily increasing complexity
Processing heavy tasks on main thread causing freezes