Workers
Adrian Lemes
Quiz
JavaScript is ________________
a) Asynchronous
b) Single Thread
c) Both
d) Confused
Event loop
Single Thread
“Every line of front end JS you've ever written
has (momentarily) stopped your page from
working”. Nor Churchill - Neither Mark Twain
60 frames / second
❏ The users expected pages more simple and interactive
❏ interactions must be fluid
❏ The current devices refresh the view 60 times per second
❏ 1 second == 1000ms
60 frames == x
❏ Each frame has only 16ms to execute
❏ We should worry about what execute and not block the main
thread
Workers
❏ A worker is an object created using a constructor
that runs a named JavaScript file in the
background.
❏ Has its own global execution context, self !==
window
❏ Communicate to the main thread via the
postMessage API
❏ Web Worker (parallelism) x Service Worker
(offline) x Worklets (Browser render journey)
Web
Workers
Web Workers
❏ The W3C published a first draft of the web
workers standard in 2009
❏ Web workers is an asynchronous system, or
protocol, for web pages to execute tasks in
the background, independently from the main
thread and website UI
❏ Best suited to execute long-running or
demand computation tasks
❏ Helps to free main thread to user interactions
❏ Angular 8 has built-in support
Main Thread
Web Worker
PostMessage
❏ Using a web worker:
const myWorker = new Worker('worker.js');
❏ Sending a message to worker
myWorker.postMessage('Hello!');
❏ Receiving result from worker
myWorker.onmessage = function(e) {
console.log(e.data);
}
main.js
❏ Receiving message from main.js
self.onmessage = function(e) {
console.log(e.data);
// Send message to main file
self.postMessage(workerResult);
}
worker.js
❏ Navigator object (browser infos)
❏ Location object (Read Only)
❏ XMLHttpRequest (Service worker uses Fetch)
❏ setTimeout()/clearTimeout() and setInterval()/clearInterval()
❏ The application cache
❏ Parsing data
❏ Virtual DOM
❏ File Reader, blob, ArrayBuffer
❏ importScripts() method to import other scripts
❏ Spawning other workers
❏ Not available DOM and Window objects
What we can do
❏ It’s not possible to specify if a web worker should run on a specific
core
❏ Web workers on a single core will not run in parallel but will not block
the main UI thread
❏ On a multicore CPU Web Workers can truly run in parallel if the SO
decides to
Web Workers and Cores
Service
Workers
What is service worker?
❏ Service worker is a programmable proxy,
allowing you to control how the requests
from your page is handled
❏ Runs in background
❏ Enable cache (requests, image, etc)
❏ Enable push notification
❏ Background sync
What is service worker?
Demo

Workers

  • 1.
  • 2.
  • 3.
    JavaScript is ________________ a)Asynchronous b) Single Thread c) Both d) Confused
  • 4.
  • 5.
    Single Thread “Every lineof front end JS you've ever written has (momentarily) stopped your page from working”. Nor Churchill - Neither Mark Twain
  • 6.
    60 frames /second ❏ The users expected pages more simple and interactive ❏ interactions must be fluid ❏ The current devices refresh the view 60 times per second ❏ 1 second == 1000ms 60 frames == x ❏ Each frame has only 16ms to execute ❏ We should worry about what execute and not block the main thread
  • 7.
    Workers ❏ A workeris an object created using a constructor that runs a named JavaScript file in the background. ❏ Has its own global execution context, self !== window ❏ Communicate to the main thread via the postMessage API ❏ Web Worker (parallelism) x Service Worker (offline) x Worklets (Browser render journey)
  • 8.
  • 9.
    Web Workers ❏ TheW3C published a first draft of the web workers standard in 2009 ❏ Web workers is an asynchronous system, or protocol, for web pages to execute tasks in the background, independently from the main thread and website UI ❏ Best suited to execute long-running or demand computation tasks ❏ Helps to free main thread to user interactions ❏ Angular 8 has built-in support Main Thread Web Worker PostMessage
  • 10.
    ❏ Using aweb worker: const myWorker = new Worker('worker.js'); ❏ Sending a message to worker myWorker.postMessage('Hello!'); ❏ Receiving result from worker myWorker.onmessage = function(e) { console.log(e.data); } main.js
  • 11.
    ❏ Receiving messagefrom main.js self.onmessage = function(e) { console.log(e.data); // Send message to main file self.postMessage(workerResult); } worker.js
  • 12.
    ❏ Navigator object(browser infos) ❏ Location object (Read Only) ❏ XMLHttpRequest (Service worker uses Fetch) ❏ setTimeout()/clearTimeout() and setInterval()/clearInterval() ❏ The application cache ❏ Parsing data ❏ Virtual DOM ❏ File Reader, blob, ArrayBuffer ❏ importScripts() method to import other scripts ❏ Spawning other workers ❏ Not available DOM and Window objects What we can do
  • 13.
    ❏ It’s notpossible to specify if a web worker should run on a specific core ❏ Web workers on a single core will not run in parallel but will not block the main UI thread ❏ On a multicore CPU Web Workers can truly run in parallel if the SO decides to Web Workers and Cores
  • 14.
  • 17.
    What is serviceworker? ❏ Service worker is a programmable proxy, allowing you to control how the requests from your page is handled ❏ Runs in background ❏ Enable cache (requests, image, etc) ❏ Enable push notification ❏ Background sync
  • 18.
  • 19.