{{announcement.body}}
{{announcement.title}}

How to Use Web Workers for Non-Blocking User Interface?

DZone 's Guide to

How to Use Web Workers for Non-Blocking User Interface?

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.

· Web Dev Zone ·
Free Resource

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.

web worker

Source

Processes like encryptions of data and resizing of images involve the main thread, which on the other hand, freezes the user interface. In such cases, the users find it annoying to use the application. Web workers 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. 

Here are some of the types of use case/apps for using web workers: 

  • Complex calculations 
  • Real-time content formatting
  • Progressive web app
  • Extensive data update on a database 
  • Image filtering

3 Common Issues That Applications Struggle With and How Web Workers Convert Them Into a Success

These three common problems include resizing of images, Ray tracing, and encryptions of data. What do these three cases have in common? All these operations have heavy computations to perform by CPU in fractions of seconds in the background. Configuring your app component to web workers makes it trouble-free and frees up the user interface. 

Let’s understand in detail each case:

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. The very common example of Ray tracing is it being used in gaming applications.

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.

Having discussed these use cases, you might have understood the idea of using web workers in different cases of complex computations. From resizing of an image to computer gaming applications, all that involve complex programming yet a mandatory non-blocking UI experience. With the advent of real-time applications and more intelligent software web apps, the availability of Web workers like technology is a boon to the programming world. You can do as many complex processes as you want to by separating the processes into a different thread and still maintain a smooth operation of the user interface.

Topics:
angular, angular application development, angular applications, frontend framework, javascript, tutorial, web dev, web developement, web workers, website (industry)

Published at DZone with permission of Hiren Dhaduk . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}