This page looks best with JavaScript enabled

Parallelize Work with Workers in Javascript

 ·   ·  ☕ 2 min read

Use workers when doing lot of work that can be spawned off in another “thread”.

How do you open parallel work threads today? Possibly using async functions?

Async functions do not block the main thread but allow processing to take place in parallel. Well, almost in parallel - see how event loops work.

Although async functions are non-blocking, the call back functions included therein do block the thread. One of the ways of managing truly async functions is through “workers”.

Using workers is a way of creating a “parallel” thread to process a large body of work, while maintaining the main thread going with its own thing.

You can do this pretty easily -

  • Include your business logic that takes considerable CPU/time in another file.
  • Call the script using worker

This is how the worker looks..

1
2
3
4
// work_a_lot.js
const onmessage = evt => {
  processLotOfData(evt.data);
};

And, we can call the worker using any async function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// main.js

const workLot = new Worker('work_a_lot.js');
doLotOfWork();

async doLotOfWork() {
    // get data
    workLot.postmessage(data);
}

There are multiple ways in which you can utilize this method in real-world. For example -

  1. Split thousands or millions of records into blocks, and call five workers to process data (change data, validate them as per new rules etc.)
  2. Call file operations to batch process files FTP’d to the server (FTP, really? This is 2019)
  3. Invoke independent API calls to communicate with external systems for a specific transaction
Stay in touch!
Share on

Prashanth Krishnamurthy
WRITTEN BY
Prashanth Krishnamurthy
Technologist | Creator of Things