↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Using Web Workers to Speed-Up Your JavaScript Applications

The post argues that Web Workers help browser-based JavaScript applications stay responsive by moving expensive work to background threads. It contrasts single-threaded blocking with multi-threaded execution, then shows the basic worker lifecycle: create a worker file, send messages to it, receive replies, and terminate it when the task is done.

Reading notes
#

  • JavaScript in the browser usually runs on a single thread, so a long task can block everything else until it finishes.
  • Web Workers let the browser run JavaScript in separate threads, which helps prevent frozen user interfaces during resource-intensive work.
  • A worker is created by loading a separate JavaScript file with new Worker('work.js').
  • The main application starts communication with postMessage().
  • A worker listens for incoming data with a message event listener.
  • A worker sends data back with self.postMessage().
  • The main application can listen for replies from the worker with its own message event listener.
  • A worker can be stopped from the main application with worker.terminate().
  • A worker can stop itself with self.close().
  • Worker scripts must come from the same domain and protocol as the page that creates them.
  • Workers do not have access to the DOM, document, window, or parent.
  • If a worker needs to affect the UI, the main application must receive the data and update the interface itself.
  • Web Workers do not work when a page is opened directly from file://; the post says a local development server is needed instead.