Open EasonWang01 opened 4 years ago
You are sending 1 task to microjob and 4 separate tasks to worker.
I updated your example code for microjob so that it actually creates 4 worker threads. The speed difference results are mostly negligible.
Fixed
(async () => {
const { job, start, stop } = require("microjob");
try {
// start the worker pool
console.time("microjob");
await start();
const threadCount = 4;
const promises = []
for (let i = 0; i < threadCount; i++) {
// this function will be executed in another thread
const resprom = job(() => {
const result = [];
const totalDataLength = 2000000;
// heavy CPU load ...
const crypto = require("crypto");
const sha256 = (s) => crypto.createHash("sha256").update(s).digest();
const shaArray = Array.from(Array(totalDataLength / threadCount)).map((num) =>
sha256(String(num))
);
result.push(shaArray);
return result;
}, { ctx: { threadCount } });
promises.push(resprom)
}
var res = await Promise.all(promises)
console.log(res);
console.timeEnd("microjob");
} catch (err) {
console.error(err);
} finally {
// shutdown worker pool
await stop();
}
})();
microjob version:
15375.269ms
Node.js native multi-thread version
5311.715ms