DEV Community

loading...

Web Workers in Action: When to use them?

Hiren Dhaduk
A versatile leader with experience in assisting successful companies extend their tech capabilities.
・3 min read

The best about working with Angular is that they support Web Workers. When you're dealing with CPU intensive computations, web workers are of great help.

They put these complex processes into a separate thread to avoid the involvement of the main thread in the explicit background processes and maintain an effortless operation of the user interface.

This is one of the major reasons why they play a significant role in increasing Angular performance. However, web workers offer different benefits and limitations depending on each case. Let’s discuss three distinct use cases of web workers:

Use case 1:

Consider resizing an image on your Angular application. Although it’s not a big task, it requires multiple calculative operations to be performed in a fraction of seconds. For instance, calling the resize method and passing the objects referring to the Javascript Image Manipulation Program along with the height and width parameters. Consequently, these complex computations involving the main thread of application freeze the user interface.

Here’s where web workers come into the picture. It distributes the computations across separate threads, reducing the burden of the main thread. This process does not block the UI but further continues the process in the background without hindering the user experience.

Use case 2:

Ray tracing is a rendering technique that uses a heavy intensive CPU mathematical computation. It traces the light as pixels to generate an image and stimulates the lighting effects like reflection, refraction, and many others. All these computations involve multiple threads to operate, and this leads to the blocking of the user interface. To keep the user interface running effortlessly, we need a separate thread that only works for ray tracing.

Web workers split the image rendering between several workers and also between CPUs as required. Having said that, the background processes become light weighted and do not block the user interface. Though Web workers are not commonly used, they perform the important functions in massive computations development.

Use case 3:

Let’s take an example of banking transactions and other financial transactions that require a high level of secure encryptions. Though these transactions are managed easily with the sleek user interface. Despite executing hundreds of transactions simultaneously, it provides a completely polished UI interaction.

To perform the end-to-end encryptions for sensitive data, we need some concrete business logic that justifies the time, coding efforts, and user experience. It becomes time-consuming and adds more complexity for larger projects with giant data.

Web workers manage these processes since they are the backbone of performing CPU intensive operations. They free the main thread and continue the process in the background. It is much advantageous when you want to perform such complex computations. Web workers do their job here to solely process the calculations for encrypting the data and run the algorithms.

Conclusion

Having discussed these use cases, you can make a solution to execute complex computations and processes with more speed and accuracy. These problems may seem rare but can be the bottleneck time-consuming parts of the large scale apps when it comes to faster deliveries and continuous integration.

The great thing is, they have made it more accessible to use web workers in the latest Angular versions. You can arbitrarily choose the name and locations except keeping the same name and folder as your component.

Discussion (0)