Badass JavaScript

A showcase of awesome JavaScript that pushes the boundaries of what's possible on the web, by @devongovett.

Parallel.js: A JavaScript Multicore Processing Library Using Web Workers

February 26th 2013

Parallel.js is a very nice looking wrapper around JavaScript Web Workers making some interesting applications of multicore/parallelized processing in JavaScript much easier.  Usually with Web Workers, you must write a totally separate script and load it into the worker thread.  This isn’t very convenient in some cases, especially when you need to share code between the main and worker threads.  Parallel.js solves that problem and provides a nice API abstraction to perform common tasks.

Parallel.js allows you to spawn a worker containing one or more functions, defined in your parent thread rather than as a separate file.  You just pass the function names and some arguments to call the function with, and Parallel.js will spawn a worker thread, run the function with the passed arguments, and send the result back to the main thread asynchronously using a Promise based API.

There is also a MapReduce API for processing large datasets that will split the data into chunks to be processed in parallel by an arbitrary number of worker threads.  When all of the processing is complete, the result is merged back together and returned to the main thread.

As web applications do more of their processing on the client side, we will need to take advantage of the multicore machines that modern systems have in order to maintain a good user experience.  As a rule of thumb, we never want to do a lot of processing on the main UI thread since the user will notice the lag.  Web Workers are great, but in general quite difficult to use.  I’m glad to see libraries beginning to make things easier in this department.

As always, you can check out Parallel.js on Github and its website.