Bird
0
0

You want to offload a CPU-heavy calculation to a Web Worker in Angular and update the UI with the result. Which approach correctly handles this?

hard📝 component behavior Q8 of 15
Angular - Performance Optimization
You want to offload a CPU-heavy calculation to a Web Worker in Angular and update the UI with the result. Which approach correctly handles this?
ACall the calculation function inside the worker and update UI from the worker.
BRun the calculation in the main thread and update UI directly.
CUse a service to run the calculation synchronously and update UI.
DCreate a Web Worker, send data with postMessage, listen for onmessage to update UI.
Step-by-Step Solution
Solution:
  1. Step 1: Understand Web Worker communication pattern

    Data is sent to the worker using postMessage, and results are received via onmessage.
  2. Step 2: Recognize UI update constraints

    UI updates must happen in the main thread, so the main thread listens for worker messages and updates UI accordingly.
  3. Final Answer:

    Create a Web Worker, send data with postMessage, listen for onmessage to update UI. -> Option D
  4. Quick Check:

    Worker handles calculation; main thread updates UI [OK]
Quick Trick: Send data to worker, receive result, update UI in main thread [OK]
Common Mistakes:
  • Trying to update UI directly from worker
  • Running heavy tasks on main thread
  • Using synchronous services for heavy computation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes