Delegating Consuming Task into WebWorker<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Delegating Consuming Task into WebWorker

Posted on: March 23, 2017

Heavy operation can hang the main thread in JavaScript. This is mainly because there is only a single thread and every operation needs to not be too much intensive. A way to handle an heavy operation is to delegate this one into a WebWorker.

A WebWorker is a place in the browser that can be requested by a page to do some task without being executed into the main thread. It has some limitations, like not being able to manipulate the Dom and being more slow, but has the main advantage to not interfere the main thread. All is done by messages between the script from the page and the script that run the worker.

To illustrate the benefit, we will use an intensive script that will be run in the main thread from one button and having a second button that execute the same script into the WebWorker. The main thread will have the time updating every 50ms as well as adding a dot. The first button will have the consequence of having the time and the dot to stop for few seconds, while the WebWorker will have always a smooth time and dot update. Here is the result of the code being executed in the main thread.

As you can see, when the button is click, the heavy process is taking so much of the processor that it hangs the whole thread. The clock stops ticking, the dots stop moving. However, looks at the result with WebWorker.

The animation keeps working with the dots and the clock is not interfered by calculation. At the end, it's more smooth because the main thread is not doing the heavy lifting -- the WebWorker is doing it.

The whole project is built in TypeSript which compile everything into JavaScript. The whole code can be found in this GitHub repository:

The project contains 3 files. The main.ts which will create the two buttons and starts the timer for the clock and the dots. The algo.ts which contains an intensive, not efficient, way to calculate prime numbers. It has two parameters, the first is the number of iteration and the second a multiplier. The iteration is to find more than just one prime number, the multiplier is increasing with a random number a number to verify if it's a prime.

The slow button is executing the following code that disable the button, instantiate the algorithm class and execute it with a huge number. When it returns, it says that the data is back and enable the button.

1function slowFunction(): void {
2 $("#btn1").prop("disabled", true);
3 const algo = new Algorithm();
4 const result = algo.calculatePrimes(50, 99887766554);
5 const $line = $("<p>").append("Result is returned");
6 $("#main").append($line); $("#btn1").prop("disabled", false); }

The WebWorker button is doing something different and needs more code. To execute code in the WebWorker we need to load the WebWorker and pass a message. To Receive the value back to the page from the WebWorker, we need to subscribe to an event.

1let worker: Worker;
2const callBack = (message: MessageEvent) => {
3 if ( === "done") {
4 const result =;
5 const $line = $("<p>").append("Result is returned");
6 $("#main").append($line); }
7 $("#btn2").prop("disabled", false);
8 };
10 function slowFunctionWebWorker(): void {
11 $("#btn2").prop("disabled", true);
12 if (worker) {
13 worker.removeEventListener("message", callBack);
14 worker.terminate();
15 }
16 worker = new Worker("output/webworker.js");
17 worker.addEventListener("message", callBack, false);
19 setTimeout(() => {
20 worker.postMessage({ multiplier: 50, iterations: 50000, });
21 }, 1000);
22 }
23 }

The call back, is looking for which message is sent back. The string could have been anything, the important is that the WebWorker needs to send the same. This is the way to handle multiple messages and response between the page and WebWorker. In our case, we have only one method and one response so a single string is the only thing required which is called "done".

The button is disabled and make sure that if a previous worker existed to remove the response listener and termine this one. After, it creates the worker. This is done by using the Worker class and passing a relative path to the WebWorker JavaScript file. After, we need to subscribe to a worker which of type "message". This allows to pass messages between the page and the WebWorker. Finally, there is a timeout of 1 sec before passing the request command down. The reason is that we need some time to the WebWorker to instanciate and register to listen to command. As you can see the postMessage is the way to call the WebWorker with some arguments.

The WebWorker is using the same algorithm that the one execute in the main script which was loading it from an other module. To do that with WebWorker, we need to get more barebone which mean we cannot use the import syntax, but directly use requirejs.

3requirejs.config({ baseUrl: ".", });
5function (algo_1) {
6 console.log("required"); //debugger;
7 self.addEventListener("message", (message: MessageEvent) => {
8 console.log("receive message begin");
9 const origin = message.origin;
10 const iterations =;
11 const multiplier =;
12 const algo = new algo_1.Algorithm();
13 const result = algo.calculatePrimes(iterations, multiplier);
14 console.log("receive message end");
15 (self as any).postMessage({ command: "done", primes: result });
16 });

To load a script, we must use importScripts which will let us having requirejs to load the module. Then, we need to configure RequireJs with a minimum configuration. Then, we call require to load the algo module. A good thing is that you can use console.log as well as the debugger keyword to debug the WebWorker. Next, you need to subscribe to the "message" event to be able to receive the postMessage from the webpage. Once we receive a message, we get the parameters, instantiate the algorithm class and wait the result. Once the result is out, we postMessage back to the page. I had to cast to any to have access to postMessage (there is probably a better way).

The end result is way better and as you can see, the interface doesn't pay the price. However, if you have keen eyes, you also saw that I reduced the multiplier count from 99 887 766 554 to 50 000. That is a lot less! Why? Because keeping the same huge value was killing the WebWorker. By that, I mean that the algorithm was running forever. The whole process was running more slowly than the main thread for an unknown reason to me (at this time I am writing this post). I'll have a future post about why it's slower, but for the moment, if you need to have some telemetry task, or small task that block the UI just for more than few seconds (but not too much!), it may be worth it to move some logic down to a WebWorker. Also, as you can see, TypeScript is totally possible with WebWorker without a lot of hassle which is a big plus for anyone working with TypeScript project.