node package manager



Parallel Computing with Javascript

Parallel.js is a simple library for parallel computing in Javascript, either in Node.js or in the Web Browser. Parallel takes advantage of Web Workers for the web, and child processes for Node.


You can download the raw javascript file here

Just include it via a script tag in your HTML page

Parallel.js is also available as a node module:

npm install paralleljs


Parallel(data, opts)

This is the constructor. Use it to new up any parallel jobs. The constructor takes an array of data you want to operate on. This data will be held in memory until you finish your job, and can be accessed via the .data attribute of your job.

The object returned by the Parallel constructor is meant to be chained, so you can produce a chain of operations on the provided data.


  • data: This is the data you wish to operate on. Will often be an array, but the only restrictions are that your values are serializable as JSON.
  • options (optional): Some options for your job
    • evalPath (optional): This is the path to the file eval.js. This is required when running in node, and required for some browsers (IE 10) in order to work around cross-domain restrictions for web workers. Defaults to the same location as parallel.js in node environments, and null in the browser.
    • maxWorkers (optional): The maximum number of permitted worker threads. This will default to 4, or the number of cpus on your computer if you're running node
    • synchronous (optional): If webworkers are not available, whether or not to fall back to synchronous processing using setTimeout. Defaults to true.


var p = new Parallel([1, 2, 3, 4, 5]);
console.log(; // prints [1, 2, 3, 4, 5] 


This function will spawn a new process on a worker thread. Pass it the function you want to call. Your function will receive one argument, which is the current data. The value returned from your spawned function will update the current data.


  • fn: A function to execute on a worker thread. Receives the wrapped data as an argument. The value returned will be assigned to the wrapped data.


var p = new Parallel('forwards');
// Spawn a remote job (we'll see more on how to use then later) 
p.spawn(function (data) {
  data = data.reverse();
  console.log(data); // logs sdrawrof 
  return data;
}).then(function (data) {
  console.log(data) // logs sdrawrof 


Map will apply the supplied function to every element in the wrapped data. Parallel will spawn one worker for each array element in the data, or the supplied maxWorkers argument. The values returned will be stored for further processing.


  • fn: A function to apply. Receives the wrapped data as an argument. The value returned will be assigned to the wrapped data.


var p = new Parallel([0, 1, 2, 3, 4, 5, 6]),
    log = function () { console.log(arguments); };
// One gotcha: anonymous functions cannot be serialzed 
// If you want to do recursion, make sure the function 
// is named appropriately 
function fib(n) {
  return n < 2 ? 1 : fib(- 1) + fib(- 2);
// Logs the first 7 Fibonnaci numbers, woot! 


Reduce applies an operation to every member of the wrapped data, and returns a scalar value produced by the operation. Use it for combining the results of a map operation, by summing numbers for example. This takes a reducing function, which gets an argument, data, an array of the stored value, and the current element.


  • fn: A function to apply. Receives the stored value and current element as argument. The value returned will be stored as the current value for the next iteration. Finally, the current value will be assigned to current data.


var p = new Parallel([0, 1, 2, 3, 4, 5, 6, 7, 8]);
function add(d) { return d[0] + d[1]; }
function factorial(n) { return n < 2 ? 1 : n * factorial(- 1); }
function log() { console.log(arguments); }
// Approximate e^10 (n) { return Math.pow(10, n); }).reduce(add).then(log);

then(success, fail)

The functions given to then are called after the last requested operation has finished. success receives the resulting data object, while fail will receive an error object.


  • success: A function that gets called upon succesful completion. Receives the wrapped data as an argument.
  • failure (optional): A function that gets called if the job fails. The function is passed an error object.


var p = new Parallel([1, 2, 3]);
function dbl(n) { return n * 2; } (data) {
  console.log(data); // logs [8, 16, 24] 
// Approximate e^10 (n) { return Math.pow(10, n) / factorial(n); }).reduce(add).then(log);


If you have state that you want to share between your main thread and the worker threads, this is how. Require takes either a string or a function. A string should point to a file name. Note that in order to use require with a file name as an argument, you have to provide the evalPath property in the options object.


var p = new Parallel([1, 2, 3], { evalPath: '' });
function cubeRoot(n) { return Math.pow(n, 1 / 3); }
// Require a function 
// Require a file 
p.require('blargh.js'); (d) {
  return blargh(20 * cubeRoot(d));


browser support