Frontend Shots - Web Workers

Introduction

 
Frontend Shots is a series of posts where I shall try to explain various important concepts in frontend web development landscape. This post covers a topic which I understood very recently although it has been around for more than a decade!
 
Web Workers. What are they? Why do we need them? Let’s get the answers.
 

What are Web Workers?

 
Imagine you have been given a task to fill water in a container of capacity 2 litres using a mug. You can do that easily within a few seconds.
 
Now suppose you are asked to next fill up a container of capacity 100 litres using the same mug. The idea of it will make you sweat! Instead of doing the work all alone, you request your siblings to help you and you delegate it to them so that you can do some other important work in the meanwhile.
 
In the JavaScript universe, Web Workers can be thought of as something like that. Web Workers can run scripts doing computation-heavy tasks in the background and can communicate with the main thread without freezing the user interface. Browsers first introduced Web Workers in around 2009 to allow programmers to run heavy CPU intensive tasks concurrently utilizing the multi-cores of the processor. Web Workers are not part of JavaScript. It is an API provided by web browsers just like the browser’s Web APIs. Web Workers can do a lot of cool stuff like doing network requests, accessing some window objects like Navigator, XMLHttpRequest, Array, Date, Math, String, window timers. However, they cannot directly access the DOM. Web Workers can be thought of as an independent JavaScript environment running in a parallel universe and can communicate with the main JavaScript universe.
 
Web Workers are also known as “Dedicated Workers”.
 

Why Web Workers?

 
Whenever any task is performed that requires heavy computation, it makes the UI irresponsive, making it non-interactive. This is not a great experience for end-users as they no longer can interact with the UI until the task is executed. A lot of users still use low-end feature phones that do not up top-level hardware specs. Running heavy CPU intensive operations on such devices often result in an undesirable experience for users. Even a for loop operation can make the UI unresponsive until it has completed execution.
 
Web Workers come in extremely handy when it comes to optimizing performance and keeping the UI very responsive. The actual time taken for performing the task remains almost the same, however, the user experience is enhanced to a great extent since the user is not blocked from doing any task.
 

How to use Web Workers?

 
Let’s see how a simple code such as Calculating the Fibonacci number at a specific position can make the UI unresponsive and how we can enhance the user experience to a great extent using Web Workers.
 
index.html
  1. <!DOCTYPE html>  
  2. <html>  
  3.   <head>  
  4.     <meta charset="utf-8" />  
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge" />  
  6.     <title>Web Workers Demo</title>  
  7.     <meta name="viewport" content="width=device-width, initial-scale=1" />  
  8.     <link rel="stylesheet" type="text/css" media="screen" href="main.css" />  
  9.   </head>  
  10.   <body>  
  11.     <main>  
  12.       <h1>Fibonacci Calculator</h1>  
  13.       <label for="position">Position</label>  
  14.       <input type="text" placeholder="Enter a position" id="position" />  
  15.       <button type="button" onclick="showResult(position)">  
  16.         Calculate Fibonacci  
  17.       </button>  
  18.       <p id="answer"></p>  
  19.     </main>  
  20.   
  21.     <script>  
  22.       function calculateFibonacci(num) {  
  23.         if (num < 2) {  
  24.           return num;  
  25.         } else {  
  26.           return calculateFibonacci(num - 1) + calculateFibonacci(num - 2);  
  27.         }  
  28.       }  
  29.       function showResult(num) {  
  30.         const answerEl = document.getElementById('answer');  
  31.         const position = Number(document.getElementById('position').value);  
  32.         const button = document.querySelector('button');  
  33.         button.disabled = true;  
  34.         button.innerText = 'Loading...';  
  35.         const answer = calculateFibonacci(position);  
  36.         answerEl.innerText = `The Fibonacci number at position ${position} is ${answer}`;  
  37.         button.disabled = false;  
  38.         button.innerText = 'Calculate Fibonacci';  
  39.        }  
  40.     </script>  
  41.   </body>  
  42. </html>  
main.css
  1. #position {    
  2.     border: 1px solid #c1c1c1;    
  3.     padding: 4px 16px;    
  4.     font-size: 18px;    
  5.     display: block;    
  6. }    
  7.     
  8. button {    
  9.     border: none;    
  10.     display: block;    
  11.     margin-top: 8px;    
  12.     padding: 8px 16px;    
  13.     background-color: blueviolet;    
  14.     color: white;    
  15.     font-weight: bold;    
  16.     cursor: pointer;    
  17. }    
  18. button:disabled {    
  19.     opacity: 0.5;    
  20.     cursor: not-allowed;    
  21. }     
The above code creates an input text box that accepts any number as a position to calculate the Fibonacci number at that position. I have deliberately kept the calculateFibonacci code unoptimized as of now.
 
Try running this code and enter a high value such as 45 and try to calculate the Fibonacci. The UI instantly becomes unresponsive and cannot be interacted unless the function execution is complete.
 
Let’s try improving the user experience by simply using Web Workers and not doing any kind of optimization to the Fibonacci function.
  1. <!DOCTYPE html>  
  2. <html>  
  3.   <head>  
  4.     <meta charset="utf-8" />  
  5.     <meta http-equiv="X-UA-Compatible" content="IE=edge" />  
  6.     <title>Web Workers Demo</title>  
  7.     <meta name="viewport" content="width=device-width, initial-scale=1" />  
  8.     <link rel="stylesheet" type="text/css" media="screen" href="main.css" />  
  9.   </head>  
  10.   <body>  
  11.     <main>  
  12.       <h1>Fibonacci Calculator</h1>  
  13.       <label for="position">Position</label>  
  14.       <input type="text" placeholder="Enter a position" id="position" />  
  15.       <button type="button" onclick="showResult(position)">  
  16.         Calculate Fibonacci  
  17.       </button>  
  18.       <p id="answer"></p>  
  19.     </main>  
  20.   
  21.     <script>  
  22.       async function showResult(num) {  
  23.         const answerEl = document.getElementById('answer');  
  24.         const position = Number(document.getElementById('position').value);  
  25.         const button = document.querySelector('button');  
  26.         button.disabled = true;  
  27.         button.innerText = 'Loading...';  
  28.          if (window.Worker) {  
  29.           const worker = new Worker('fibonacci.js');  
  30.           worker.postMessage(position);  
  31.           worker.addEventListener('message', ({ data }) => {  
  32.             answerEl.innerText = `The Fibonacci number at position ${position} is ${data}`;  
  33.             button.disabled = false;  
  34.             button.innerText = 'Calculate Fibonacci';  
  35.             worker.terminate();  
  36.           });  
  37.         }   
  38.       }  
  39.     </script>  
  40.   </body>  
  41. </html>  
Let’s see what is happening.
 
First, we check if Web Workers are available or not (Although Web Workers are supported by almost all browsers, it’s better to check).
  1. window.Worker // checks if Worker is available  
A worker is created using the Worker constructor function.
  1. const worker = new Worker('fibonacci.js')   
A worker is a script file that needs to be created and the path to the file needs to be provided while creating the worker instance.
 
The Web Worker API provides some events and methods to transfer data between the worker and the main script.
  • postMessage is a method that can be called on the worker to transfer data from the main script to the worker.
  • message is an event that can be attached to the worker. This provides data from the worker. The data is stored in a ‘data’ object which I have destructured to get the data from the worker.
Now let’s see the code in the worker:
  1. function calculateFibonacci(num) {  
  2.   if (num < 2) {  
  3.     return num;  
  4.   } else {  
  5.      return calculateFibonacci(num - 1) + calculateFibonacci(num - 2);  
  6.   }  
  7. }  
  8.   
  9. self.addEventListener('message', (e) => {  
  10.   const answer = calculateFibonacci(e.data)  
  11.   self.postMessage(answer);  
  12. });  
The code is pretty self-explanatory. In the worker file, we use the message event to listen for data passed from the main thread and perform calculations here.
 
The result of the operation is transferred back to the main thread via the postMessage method.
 
We can also listen for any errors happening during this communication using the error event listener.
 
The above code for calculating Fibonacci can be optimized by using some dynamic programming concepts (in simple terms by storing results in a dictionary or a cache).
  1. const cache = {};  
  2. function calculateFibonacci(num) {  
  3.   if (num < 2) {  
  4.     return num;  
  5.   } else {  
  6.     if (!cache[num]) {  
  7.       cache[num] = calculateFibonacci(num - 1) + calculateFibonacci(num - 2);  
  8.       return cache[num];  
  9.     } else {  
  10.       return cache[num];  
  11.     }  
  12.   }  
  13. }  
  14.   
  15. self.addEventListener('message', (e) => {  
  16.   const answer = calculateFibonacci(e.data);  
  17.   setTimeout(() => {  
  18.     self.postMessage(answer);  
  19.   }, 3000);  
  20. });  
This will drastically reduce the recursion steps and almost instantly produce the results even for higher numbers.
 

Conclusion

 
This is the basic concept of Web Workers and how they can be used to build performant apps and offer better user experience. The example I provided is very basic.
 
However, Web Workers can be used on various occasions such as:
  • Doing image processing inside the canvas.
  • Prefetching data from the network to provide a faster and seamless user experience.
  • Doing end-to-end data encryption, etc.
My idea was to cover the basics of Web Workers in this post which should be a part of a frontend developer’s roadmap. There are few concepts such as Shared Workers which are almost the same as that of dedicated workers but can be shared among script belonging to the same origin (for eg different tabs of the same origin).
 
The MDN Docs is a great place to know more about Web Workers and understand the ways in which they can be used.
 
Here is another great post on the basics of Web Workers.
 
Have a great one!


Similar Articles