Web is a quirky platform, while very inviting, can cause serious heartburn when under stress.

One important thing to note in here is that the browser cannot refresh the screen when it’s main thread is busy performing some activity. What this means is the page looks stuck. And if it goes on long enough, we get this:

Needless to say, it kills UX.

Over time we came up with ways to tame the beast and it works for most. Performing time consuming operations asynchronously can solve some of the problems when the operations themselves are not resource intensive; as in long polling a server for updates or fetching fresh data based on user action.

All async means here is that it will allow the calling function to wait for the result. Like while fetching list of comments, show some loading gif. Once fetched, remove gif and paint screen with comments.

Here, the browser makes a request and simply waits for the server to respond while keeping the passed in callback function in the callback queue.

Once it receives a response, the JS engine will invoke the callback with the data and you can go about rendering it on screen.

Because the process of issuing an XMLHttpRequest or rendering the results on screen is not resource intensive, we see no page freezes. So far so good.

However it all goes for a toss when you need to perform some resource intensive calculation: like maybe sorting a very large array of complex data. Now, even if it is async, JavaScript is still single threaded.

One can play around with

setTimeout(cb, 0);

All this will do is it will pop our sorting function from the callback queue and push it to the call stack whenever the call stack is empty.

It will then begin it’s operation blocking the main thread. And as the main thread is blocked, the page will feel unresponsive.

Say we have a React component called App that handles the sort button like this:

And we have a SortService as this:

Agreed, bubble sort with it’s O(n^2) . But bear with me for a moment; I am trying to make a point.

As is evident, this sort method will be slightly slow when we have like 20000 items in the list and fall dead from the sky when we have like 40000.

Yes, one can always slice their list and there can be other modifications, but point being there are times when calculations can become heavy.

Like may be we are attempting to perform some image processing or long polling the server every 5 seconds ( no, we haven’t heard of WebSockets :) ).

Every time we attempt to do any of these, the main thread will be blocked and users will not be able to interact with your app. Pretty bad for user experience.

Enter WebWorkers. According to the MDN page:

Web Workers makes it possible to run a script operation in background thread separate from the main execution thread of a web application. The advantage of this is that laborious processing can be performed in a separate thread, allowing the main (usually the UI) thread to run without being blocked/slowed down.

Let us create a file called app.worker.js and place it in /src :

As evident, we have simply copied our sort logic inside a message event listener.

WebWorkers need a slight bit of help before it can be a part of our webpack bundle. Let’s create a file called WebWorker.js :

Now bring it all together in our App component like this:

All of these have to be done because a WebWorker needs to be instantiated as a file, but we want it to be a part of our webpack bundle. As in, we don’t want to run the worker as a separate app with it’s own build process.

Importing the app.worker.js makes it a part of our project’s sourcetree and webpack will bundle it correctly.

In WebWorker.js we simply toString our worker code and make it into a web URL

…

In App.js , we simply create an instance of worker once the components finish mounting and in the handleSort , we issue a postMessage to our worker.

…

Think of the WebWorker as a logically separate app, like the way we treat iframes . And the way to interact with an independent app is using postMessage and adding an message handler in the app being communicated with.

In the worker, we add a listener for message event.

The worker accepts a payload of a list of users and performs some operation on it and finally, issues a postMessage with the freshly updated data as the payload.

All the time required for performing the operation is handled in another thread keeping the main thread free for responding to user interactions.

…

I have put together a small demo.