Improve Performance with Web Workers
Tags: angular, javascript, webdev, tutorial
Published: 2019-06-19 02:39:17

---
title: Improve Performance with Web Workers
published: true
tags: angular,javascript,webdev,tutorial
canonical_url: https://medium.com/@erxk_verduin/improve-performance-with-web-workers-497931fdef1b
---

#### Using the Angular 8 CLI

### Introduction

This article will illustrate how to improve start-up performance using Web Workers. Our example will be an Angular 8 application. Using the Angular 8 CLI simplifies getting started with Web Workers. However, using Web Workers is not specific to Angular and most of these concepts can be utilized in any Javascript or Typescript application.

[Download the Source Code 🚀](https://github.com/Everduin94/Web-Worker-Demo)

We will cover

- Measuring Performance in Lighthouse
- Getting Started with Web Workers in Angular 8 [[1]](https://angular.io/guide/web-worker)
- Measuring Performance with Web Workers
- Web Worker Limitations and Pitfalls

![](https://cdn-images-1.medium.com/max/799/1*PjG4nKh1rPSMM1WWCjoRVw.png)

### Measuring Performance with Lighthouse

First, we need to get a baseline measurement to gauge how performant our application is on start-up without a Web Worker. Note, we are running our Angular application in production mode — This affects start-up performance.

In the Google Chrome Developer Tools 🛠, using Lighthouse, we can measure the performance of our webpage on startup [[2]](https://developers.google.com/web/tools/lighthouse/). I have added a long-running task/computation to the startup of our application (for-loop building a massive string).

![](https://cdn-images-1.medium.com/max/807/1*3y3QeULKPgDahlxnkJRPIg.png)<figcaption>Performance without Web Worker</figcaption>

When we execute a long-running task on the Main thread, our application appears to be locked up. This is because the Main thread is blocked by all of the computations in our long-running task. Thus, the user cannot interact with our application until the process has finished.

By clicking “View Trace”, we can see a visualization of CPU time on start-up. In our example, the majority of the time spent was evaluating/running our script/task. We can also verify in the trace that our code is running in the Main thread.

![](https://cdn-images-1.medium.com/max/782/1*9VaeYW8_mHbjNYfm5kIjPw.png)<figcaption>Performance without Web Worker</figcaption>

### Getting Started with Web Workers

The Angular 8 CLI has simplified getting started with Web Workers. To create a Web Worker, we will simply run the Angular 8 web-worker schematic.

![](https://cdn-images-1.medium.com/max/659/1*6chGmXLlZ2MqkhAPhDxdBg.png)<figcaption>Generate a Web Worker</figcaption>

The name and location of the worker are mostly arbitrary. A caveat, if your Web Worker has the same name and folder as your component, Angular will automatically add the following code to your component for you. If not, add this code where you want to use the worker.

{% gist https://gist.github.com/Everduin94/fbb1ac0e0d2ed8c7e683908cb7db3009 %}

The only other code generated is the actual worker itself. **This is where we will move our long-running computations to.**

{% gist https://gist.github.com/Everduin94/875d285908423e026cbcf6caa023914f %}

#### Main thread → Web Worker → Main thread

When our Web Worker invokes worker.postMessage('hello') the content inside our 'message' event listener will execute inside the Worker. Once our task has completed, postMessage(response) will be called from the Web Worker and worker.onmessage(data)=> {} will execute inside our component back in the Main thread.

{% gist https://gist.github.com/Everduin94/db596a0f6a9dc954f184387f68ec4f7e %}

### Web Worker Performance

Once we move our long-running task to the Web Worker inside of addEventListener('message', (data)=> { // Here }); we will be ready to test our performance again. Note, we will cover various limitations when executing code in a Web Worker later. For now, we simply move our code from the component to the Web Worker.

We can see that the performance of our application on start-up has significantly improved. This is because the Main thread only takes 1.8s before its finished evaluating our Javascript and rendering our components on the screen.

When our long-running task was in the Main thread, we had to wait the additional time to complete the long-running task before the application became interactive.

![](https://cdn-images-1.medium.com/max/805/1*WicKvygJstaTr58HL26sJg.png)<figcaption>Performance with Web Worker</figcaption>

The application stays interactive the entire time our script/task is running, as it is **not** on the Main thread. Performing a “View Trace” we can verify that our script/task is running in an instance of our Worker and the Main thread is sitting idle.

![](https://cdn-images-1.medium.com/max/699/1*PHCuB5C0gq034xHCxrYDJw.png)<figcaption>Performance with Web Worker</figcaption>

### Web Worker Limitations & Pitfalls

#### Cannot Pass Functions to Web Workers

Functions and methods cannot be passed to Web Workers. When an object is passed to a Web Worker, all of its methods are removed. If a function is passed to a web worker, the following exception will occur.

```
worker.postMessage(() => {return 'hello'});
```

![](https://cdn-images-1.medium.com/max/982/1*Yp92KYXiTePEkM3lnqHi-g.png)

#### Working with DOM & window

Web Workers run in a different global context than window. DOM manipulation is not allowed, and some methods and properties of the window are not available in web workers. [[3]](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers)

![](https://cdn-images-1.medium.com/max/833/1*SQ_No-02vDWyKKFN-HcbRw.png)

#### Running very large processes

In general, there is not a significant difference in time to complete a task when running in Main or in a Web Worker. If you run very large processes inside of a Web Worker, there is a point where the Web Worker becomes significantly slower than the Main thread.

In our example, if we increase the number of iterations significantly, we can see the difference in performance changes dramatically.

![](https://cdn-images-1.medium.com/max/1024/1*P00wlIWQj_Vhx12qFmgWPQ.png)<figcaption>Smaller Dataset: 19ms | 37ms</figcaption>

![](https://cdn-images-1.medium.com/max/1024/1*QUreHbRoJCTNedzD_dDpmg.png)<figcaption>Larger Dataset: 13s | 39s</figcaption>

### Summary

- Measure the performance of an application on start-up using Lighthouse in Google Chrome Developer Tools.
- Long-running tasks/computations in the Main thread will cause the UI to lock and become unresponsive
- Delegate long-running tasks to Web Workers to improve performance
- Angular 8 CLI simplifies getting started with Web Workers
- Be aware of limitations and pitfalls when working with Web Workers

### References

[1] [https://angular.io/guide/web-worker](https://angular.io/guide/web-worker)

[2] [https://developers.google.com/web/tools/lighthouse/](https://developers.google.com/web/tools/lighthouse/)

[3] [https://developer.mozilla.org/en-US/docs/Web/API/Web\_Workers\_API/Using\_web\_workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers)