A single instance of Node.js runs in a single thread. To take advantage of multi-core systems users can use Cluster.
This module uses Cluster to perform iterations using n
cores in parallel. Which can drastically decrease the time spent to perform high intensity CPU operations.
Benchmark using time
to measure the amount of seconds spent to finish the operation.
Performed using a MacBook Pro with Intel i7 2.5 GHz @ 8 Cores.
files written | singleCore.js | multiCore.js |
---|---|---|
100 | 6.569 | 1.372 |
500 | 32.600 | 6.442 |
1000 | 65.84 | 12.653 |
2000 | 133.90 | 26.217 |
4000 | 268.92 | 56.003 |
To install the most recent release from npm, run:
npm install async-cluster
Master
var ac = require('async-cluster');
var cpuCores = require('os').cpus().length;
var worker = __dirname+'/worker';
ac.eachCore(list, cpuCores, worker, callback);
Worker
module.exports = function(item, callback) {
...
callback();
};