0.0.1 • Published 7 years ago

parallel-map v0.0.1

Weekly downloads
-
License
ISC
Repository
-
Last release
7 years ago

Build Status

Parallel Map

A map function that runs on all of your CPU cores, for Node.js.

This is meant to be used with synchronous, CPU intensive function to be mapped on large arrays. parallel-map will split the load across all available CPUs.

Install

$ npm install --save parallel-map

Usage

parallel-map is asynchronous and returns a Promise. The use of forked processes necessitates that the called function resides in its own file, which should be a Node module that exports a single function.

main.js:

var parallelMap = require('parallel-map');
var path = require('path');

parallelMap([1, 2, 3, 4, 5], path.join(__dirname, 'function.js'))
  .then((results) => {
    console.log(results);
    // [1, 4, 9, 16, 25]
  });

function.js:

module.exports = function (x) {
  return x * x;
}

Note that there is an overhead to forking multiple processes to have them run in parallel, so there will only be a performance gain when the function to execute requires a lot of CPU time.