-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpuWorker.js
More file actions
52 lines (45 loc) · 1.38 KB
/
cpuWorker.js
File metadata and controls
52 lines (45 loc) · 1.38 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
const { parentPort, threadId } = require("worker_threads");
// Runs inside a worker thread, not on the main event loop.
const runCpuIntensiveComputation = (iterations) => {
const startedAt = Date.now();
let accumulator = 0;
// Intentionally CPU-heavy loop for demonstration.
for (let i = 0; i < iterations; i += 1) {
accumulator += Math.sqrt((i % 1000) + 1) * Math.sin(i % 360);
}
return {
result: Number(accumulator.toFixed(4)),
durationMs: Date.now() - startedAt,
};
};
parentPort.on("message", (payload) => {
// parentPort is the communication channel with the main thread.
const { jobId, input } = payload;
try {
const normalizedIterations = Number(input?.iterations);
if (!Number.isFinite(normalizedIterations) || normalizedIterations <= 0) {
throw new Error("Iterations must be a positive number.");
}
const { result, durationMs } =
runCpuIntensiveComputation(normalizedIterations);
// Send successful result back to WorkerPool.
parentPort.postMessage({
type: "job_completed",
jobId,
output: {
threadId,
durationMs,
iterations: normalizedIterations,
result,
},
});
} catch (error) {
// Send failure details so caller can handle it cleanly.
parentPort.postMessage({
type: "job_failed",
jobId,
threadId,
message: error.message,
});
}
});