Node.js › Clustering & Worker Threads
What is a worker thread pool, and why would you build one rather than creating a new Worker for every task?
AdvancedAnswer
Creating a new Worker has meaningful startup overhead -- a worker pool pre-creates a fixed number of threads once at startup and reuses them across many tasks, queuing work when all are busy, amortizing thread-creation cost across many tasks.
const pool = new Piscina({ filename: './cpu-task.js' });
app.post('/process', async (req, res) => {
const result = await pool.run(req.body);
res.json(result);
});Real-world example An endpoint performing CPU-intensive PDF generation uses a Piscina pool of four pre-warmed workers rather than spawning a new Worker per request, eliminating a per-request latency spike.