New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Take longer time than native worker_thread #65
Comments
EasonWang01
changed the title
Take longer time than normal single thread
Take longer time than native worker_thread
Aug 21, 2020
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. 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();
}
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
microjob version:
15375.269ms
Node.js native multi-thread version
5311.715ms
The text was updated successfully, but these errors were encountered: