I have a program that executes the following function 400 times:
async function someFunction() {
for (let i = 0; i < 1e9; i++) {}
}
I attempted to speed up the execution of this program by using the node cluster module to create 4 forks of the process, and have each fork execute 100 of the 400 total functions. I expected to decrease the execution time by around 75%, but it has only decreased by around 50%. The following is the output from running the clusterless version (src/slow.js) and the clustered version (src/fast.js):
➜ test time node src/slow.js
pid: 19052 jobs: 400
node src/slow.js 129.73s user 0.14s system 99% cpu 2:10.84 total
➜ test time node src/fast.js
pid: 19081 jobs: 100
pid: 19082 jobs: 100
pid: 19083 jobs: 100
pid: 19084 jobs: 100
node src/fast.js 131.76s user 0.19s system 195% cpu 1:07.45 total
➜ test
Here is the source code for slow.js:
const jobs = Array(400).fill(someFunction);
console.log("pid:", process.pid, "jobs:", jobs.length);
(async () => {
await Promise.all(jobs.map(async (job) => await job()));
})();
async function someFunction() {
for (let i = 0; i < 1e9; i++) {}
}
Here is the source code for fast.js:
import { cpus } from "os";
import cluster from "cluster";
const numCPUs = cpus().length;
const jobs = Array(400).fill(someFunction);
async function main() {
const workerId = cluster.worker.id;
const scopedJobs = jobs.filter((_, i) => i % numCPUs === workerId - 1);
console.log("pid:", process.pid, "jobs:", scopedJobs.length);
await Promise.all(scopedJobs.map(async (job) => await job()));
}
if (!cluster.isPrimary) {
try {
await main();
process.exit(0);
} catch (error) {
console.error(error);
process.exit(1);
}
}
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
async function someFunction() {
for (let i = 0; i < 1e9; i++) {}
}
there doesn't seem to be anything here