SlideShare a Scribd company logo
Web Worker & Service
Worker & Worklets
Web Workers
Web workers are the most general purpose type of worker. Unlike service workers, they
do not have a specific use case, other than the feature of being run separately to the
main thread. As a result, web workers can be used to offload pretty much any heavy
processing from the main thread.
Web workers are general-purpose scripts that enable us to offload processor-intensive
work from the main thread.
Main thread vs
Worker Thread
• Web workers are created using the Web Workers API. After creating a
dedicated Javascriptfile for our worker, we can add it as a
new Worker.
// Create worker
const myWorker = new Worker('worker.js');
// Send message to worker
myWorker.postMessage('Hello!');
// Receive message from worker
myWorker.onmessage = function (e) {
console.log(e.data);
}
• // Receive messagefrom main file
• self.onmessage= function (e) {
• console.log(e.data);
•
• // Send messageto main file
• self.postMessage(workerResult);
• }
Workflow of Web worker
Service
Worker
• Service workers are a type of worker that serve the explicit
purpose of being a proxy between the browser and the
network and/or cache.
•
Workflow of service Worker
Worklets
• Worklets are a very lightweight, highly specific, worker. They enable us as developers
to hook into various parts of the browser’s rendering process.
• When a web page is being rendered, the browser goes through a number of steps. I
cover this in more detail in my article on Understanding the Critical Rendering Path,
but there are four steps we need to worry about here - Style, Layout, Paint, &
Composite.
Web Worker, Service Worker and Worklets
• Let’s take the Paint stage. This is where the browser applies the styles to each element.
The worklet that hooks into this stage of rendering is the Paint Worklet.
• The Paint Worklet allows us to create custom images that can be applied anywhere CSS
expects an image, for example as a value to the background-image property.
• To create a worklet, as with all workers, we register it in our main javascript file,
referencing the dedicated worklet file.
•
• /* main.js */
CSS.paintWorklet.addModule('myWorklet.js');
• /* myWorklet.js */
registerPaint('myGradient',class{
paint(ctx,size, properties) {
var gradient= ctx.createLinearGradient(0, 0, 0, size.height / 3);
gradient.addColorStop(0,"black");
gradient.addColorStop(0.7,"rgb(210, 210, 210)");
gradient.addColorStop(0.8,"rgb(230, 230, 230)");
gradient.addColorStop(1,"white");
ctx.fillStyle=gradient;
ctx.fillRect(0,0, size.width,size.height / 3);
}
• div {
• background-image:paint(myGradient);
• }
• Overall, web workers, service workers, and worklets are all scripts that run on a
separate thread to the browser’s main thread. Where they differ is in where they are
used and features which they offers.
Browser Limitation Notes
Chrome and Opera No limit. Storage is per origin not per API
Firefox No limit. Prompts after 50 MB
Mobile Safari 50MB.
Desktop Safari No limit. Prompts after 5MB
Internet Explorer (10+) 250MB. Prompts after 10MB

More Related Content

What's hot (20)

PPT
Wsdl
ppts123456
 
PPTX
Introduction to Node js
Akshay Mathur
 
PPTX
React-JS Component Life-cycle Methods
ANKUSH CHAVAN
 
PDF
Javascript essentials
Bedis ElAchèche
 
PPTX
Ajax
Tech_MX
 
PDF
CSS Grid vs. Flexbox
Ecaterina Moraru (Valica)
 
PDF
Editor config, eslint, prettier
Samundra khatri
 
PPTX
Osgi
Heena Madan
 
PPTX
The Box Model [CSS Introduction]
Nicole Ryan
 
PPTX
React Native
Fatih Şimşek
 
PPTX
HTTP Response Codes | Errors
Siddharth Sharma
 
PPTX
Intro to React
Justin Reock
 
PDF
CSS3, Media Queries, and Responsive Design
Zoe Gillenwater
 
PPTX
Java script
Abhishek Kesharwani
 
PPTX
JavaScript
Vidyut Singhania
 
PDF
React Js Simplified
Sunil Yadav
 
PDF
JavaScript Programming
Sehwan Noh
 
PPT
Spring Framework
nomykk
 
Introduction to Node js
Akshay Mathur
 
React-JS Component Life-cycle Methods
ANKUSH CHAVAN
 
Javascript essentials
Bedis ElAchèche
 
Ajax
Tech_MX
 
CSS Grid vs. Flexbox
Ecaterina Moraru (Valica)
 
Editor config, eslint, prettier
Samundra khatri
 
The Box Model [CSS Introduction]
Nicole Ryan
 
React Native
Fatih Şimşek
 
HTTP Response Codes | Errors
Siddharth Sharma
 
Intro to React
Justin Reock
 
CSS3, Media Queries, and Responsive Design
Zoe Gillenwater
 
Java script
Abhishek Kesharwani
 
JavaScript
Vidyut Singhania
 
React Js Simplified
Sunil Yadav
 
JavaScript Programming
Sehwan Noh
 
Spring Framework
nomykk
 

Similar to Web Worker, Service Worker and Worklets (20)

PPTX
Html web workers
AbhishekMondal42
 
PDF
Service Worker 101 (en)
Chang W. Doh
 
PPTX
Workers
Adrian Caetano
 
PPTX
Web workers | JavaScript | HTML API
pcnmtutorials
 
PDF
Web workers
Surbhi Mathur
 
PPTX
Web workers
Ran Wahle
 
PDF
Yahoo! Hack India: Hyderabad 2013 | Bleeding Edge Webdev
Yahoo Developer Network
 
PDF
Bleeding edge web stuff
Niranjan Prithviraj
 
PDF
Web workers and service workers
Nitish Phanse
 
PPT
HTML5 Multithreading
Allan Huang
 
PDF
Boost your angular app with web workers
Enrique Oriol Bermúdez
 
PDF
JavaScript Web Workers
Tobias Pfeiffer
 
PPTX
Web worker
Nitin Giri
 
PDF
2013 jsdc webworker
Bingo Yang
 
PDF
Workers of the web - BrazilJS 2013
Thibault Imbert
 
PPTX
Amitesh Madhur - Web workers, HTML 5
Amitesh Madhur
 
PDF
Web Workers
IntexSoft
 
KEY
Getting Started with HTML 5 Web workers
Flumes
 
PDF
Service worker API
Giorgio Natili
 
PDF
[1C1]Service Workers
NAVER D2
 
Html web workers
AbhishekMondal42
 
Service Worker 101 (en)
Chang W. Doh
 
Web workers | JavaScript | HTML API
pcnmtutorials
 
Web workers
Surbhi Mathur
 
Web workers
Ran Wahle
 
Yahoo! Hack India: Hyderabad 2013 | Bleeding Edge Webdev
Yahoo Developer Network
 
Bleeding edge web stuff
Niranjan Prithviraj
 
Web workers and service workers
Nitish Phanse
 
HTML5 Multithreading
Allan Huang
 
Boost your angular app with web workers
Enrique Oriol Bermúdez
 
JavaScript Web Workers
Tobias Pfeiffer
 
Web worker
Nitin Giri
 
2013 jsdc webworker
Bingo Yang
 
Workers of the web - BrazilJS 2013
Thibault Imbert
 
Amitesh Madhur - Web workers, HTML 5
Amitesh Madhur
 
Web Workers
IntexSoft
 
Getting Started with HTML 5 Web workers
Flumes
 
Service worker API
Giorgio Natili
 
[1C1]Service Workers
NAVER D2
 
Ad

Recently uploaded (20)

PPTX
Destructive Tests corrosion engineer (1).pptx
zeidali3
 
DOC
MRRS Strength and Durability of Concrete
CivilMythili
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PPTX
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
PDF
ARC--BUILDING-UTILITIES-2-PART-2 (1).pdf
IzzyBaniquedBusto
 
PPTX
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
PDF
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
PDF
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
PDF
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
PPTX
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
PPTX
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
PPTX
REINFORCEMENT AS CONSTRUCTION MATERIALS.pptx
mohaiminulhaquesami
 
PDF
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
PPTX
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
PPTX
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
PDF
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
PDF
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
PDF
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
Destructive Tests corrosion engineer (1).pptx
zeidali3
 
MRRS Strength and Durability of Concrete
CivilMythili
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Break Statement in Programming with 6 Real Examples
manojpoojary2004
 
ARC--BUILDING-UTILITIES-2-PART-2 (1).pdf
IzzyBaniquedBusto
 
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
Introduction to Productivity and Quality
মোঃ ফুরকান উদ্দিন জুয়েল
 
Book.pdf01_Intro.ppt algorithm for preperation stu used
archu26
 
Unified_Cloud_Comm_Presentation anil singh ppt
anilsingh298751
 
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
Innowell Capability B0425 - Commercial Buildings.pptx
regobertroza
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
REINFORCEMENT AS CONSTRUCTION MATERIALS.pptx
mohaiminulhaquesami
 
MAD Unit - 1 Introduction of Android IT Department
JappanMavani
 
The Role of Information Technology in Environmental Protectio....pptx
nallamillisriram
 
Benefits_^0_Challigi😙🏡💐8fenges[1].pptx
akghostmaker
 
PORTFOLIO Golam Kibria Khan — architect with a passion for thoughtful design...
MasumKhan59
 
Water Design_Manual_2005. KENYA FOR WASTER SUPPLY AND SEWERAGE
DancanNgutuku
 
MAD Unit - 2 Activity and Fragment Management in Android (Diploma IT)
JappanMavani
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
Ad

Web Worker, Service Worker and Worklets

  • 1. Web Worker & Service Worker & Worklets
  • 2. Web Workers Web workers are the most general purpose type of worker. Unlike service workers, they do not have a specific use case, other than the feature of being run separately to the main thread. As a result, web workers can be used to offload pretty much any heavy processing from the main thread. Web workers are general-purpose scripts that enable us to offload processor-intensive work from the main thread.
  • 4. • Web workers are created using the Web Workers API. After creating a dedicated Javascriptfile for our worker, we can add it as a new Worker. // Create worker const myWorker = new Worker('worker.js'); // Send message to worker myWorker.postMessage('Hello!'); // Receive message from worker myWorker.onmessage = function (e) { console.log(e.data); }
  • 5. • // Receive messagefrom main file • self.onmessage= function (e) { • console.log(e.data); • • // Send messageto main file • self.postMessage(workerResult); • }
  • 7. Service Worker • Service workers are a type of worker that serve the explicit purpose of being a proxy between the browser and the network and/or cache. •
  • 9. Worklets • Worklets are a very lightweight, highly specific, worker. They enable us as developers to hook into various parts of the browser’s rendering process. • When a web page is being rendered, the browser goes through a number of steps. I cover this in more detail in my article on Understanding the Critical Rendering Path, but there are four steps we need to worry about here - Style, Layout, Paint, & Composite.
  • 11. • Let’s take the Paint stage. This is where the browser applies the styles to each element. The worklet that hooks into this stage of rendering is the Paint Worklet. • The Paint Worklet allows us to create custom images that can be applied anywhere CSS expects an image, for example as a value to the background-image property. • To create a worklet, as with all workers, we register it in our main javascript file, referencing the dedicated worklet file. •
  • 12. • /* main.js */ CSS.paintWorklet.addModule('myWorklet.js'); • /* myWorklet.js */ registerPaint('myGradient',class{ paint(ctx,size, properties) { var gradient= ctx.createLinearGradient(0, 0, 0, size.height / 3); gradient.addColorStop(0,"black"); gradient.addColorStop(0.7,"rgb(210, 210, 210)"); gradient.addColorStop(0.8,"rgb(230, 230, 230)"); gradient.addColorStop(1,"white"); ctx.fillStyle=gradient; ctx.fillRect(0,0, size.width,size.height / 3); } • div { • background-image:paint(myGradient); • }
  • 13. • Overall, web workers, service workers, and worklets are all scripts that run on a separate thread to the browser’s main thread. Where they differ is in where they are used and features which they offers.
  • 14. Browser Limitation Notes Chrome and Opera No limit. Storage is per origin not per API Firefox No limit. Prompts after 50 MB Mobile Safari 50MB. Desktop Safari No limit. Prompts after 5MB Internet Explorer (10+) 250MB. Prompts after 10MB