-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecond.js
40 lines (32 loc) · 1017 Bytes
/
second.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const { Worker } = require("worker_threads");
const doHash = async (hashInput) => {
return new Promise((resolve, reject) => {
const start = Date.now();
const worker = new Worker("./crypt.js", {
workerData: {
hashInput,
},
});
worker.once("message", (data) => {
console.log(`Worker [${worker.threadId}]: done in ${Date.now() - start}ms`);
resolve(data);
});
worker.once("error", (e) => reject(e));
});
};
const main = async () => {
const start = Date.now();
const hashes = await Promise.all([
doHash("Hash this text 500k times!"),
doHash("Hash this text 500k times!"),
doHash("Hash this text 500k times!"),
doHash("Hash this text 500k times!"),
doHash("Hash this text 500k times!"),
doHash("Hash this text 500k times!"),
doHash("Hash this text 500k times!"),
doHash("Hash this text 500k times!"),
]);
console.log(hashes);
console.log(`Main done in ${Date.now() - start} ms`);
};
main().catch(console.error);