Bird
0
0

How can you pass complex data like objects or arrays between Angular main thread and Web Worker safely?

hard📝 Application Q9 of 15
Angular - Performance Optimization
How can you pass complex data like objects or arrays between Angular main thread and Web Worker safely?
ASerialize data to JSON string manually before sending.
BUse structured cloning via postMessage; avoid functions or DOM nodes.
CSend data as global variables accessible by both threads.
DUse shared memory to directly access objects.
Step-by-Step Solution
Solution:
  1. Step 1: Recall Web Worker data transfer rules

    postMessage uses structured cloning to transfer data safely between threads.
  2. Step 2: Identify data types allowed

    Objects and arrays can be transferred, but functions and DOM nodes cannot be cloned.
  3. Final Answer:

    Use structured cloning via postMessage; avoid functions or DOM nodes. -> Option B
  4. Quick Check:

    postMessage uses structured cloning for safe data transfer [OK]
Quick Trick: postMessage clones data; avoid functions and DOM nodes [OK]
Common Mistakes:
  • Trying to share global variables between threads
  • Assuming JSON serialization is required
  • Using shared memory which is not standard in Angular

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Angular Quizzes