Sitemap
CodeToDeploy

Tech Insights | Careers | Jobs

⚡ “10 Node.js Event-Loop Power Tricks That Let You Handle Millions of Requests

3 min readDec 6, 2025

--

Node.js is fast — but only if you work with the event loop instead of fighting it.
Most performance problems in Node apps come from:

  • heavy synchronous work
  • slow I/O patterns
  • blocking JSON operations
  • misusing promises
  • poor clustering setups

Today, you’ll learn 10 event-loop hacks that high-scale teams use to push Node.js into the millions of requests per day range without adding new servers.

Let’s go. 🔥

🚀 1. Use setImmediate() Instead of setTimeout(fn, 0)

setTimeout(fn, 0) is not actually zero.
It runs on the Timers phase.
setImmediate() runs on the Check phase → sooner, more predictable.

❌ Slow

setTimeout(() => heavyJob(), 0);

✅ Fast

setImmediate(() => heavyJob());

This keeps your event loop breathing.

🚀 2. Offload CPU Tasks Using worker_threads

CPU work kills Node’s event loop.
Move it out.

const { Worker } = require("worker_threads");
function runHeavyTask(data) {
return new Promise((resolve) => {
const worker = new Worker("./heavy.js", { workerData: data });
worker.on("message", resolve);
});
}

heavy.js

const { workerData, parentPort } = require("worker_threads");
parentPort.postMessage(performCPUHeavyWork(workerData));

Event loop stays free → requests stay fast.

🚀 3. Use cluster for Vertical Scaling

One Node process ≠ one CPU core.
You paid for all cores — use them.

const cluster = require("cluster");
const os = require("os");
if (cluster.isPrimary) {
os.cpus().forEach(() => cluster.fork());
} else {
require("./server"); // your app
}

Boom: 8× throughput on an 8-core machine.

🚀 4. Avoid Blocking JSON Parsing

JSON.parse() is synchronous and expensive.

❌ Avoid

const data = JSON.parse(hugePayload);

✅ Use Streaming Parsers

const { parser } = require("stream-json");

Streaming → no event-loop freeze.

🚀 5. Use Promise.allSettled() Instead of Linear Awaits

❌ Slow (sequential)

await callA();
await callB();
await callC();

✅ Fast (parallel)

await Promise.allSettled([callA(), callB(), callC()]);

Event loop loves concurrency.

🚀 6. Prefer Streams Over Buffers

Reading large files blocks the loop.

❌ Do NOT do this:

const data = fs.readFileSync("big.mp4");

✅ Use streaming I/O:

fs.createReadStream("big.mp4").pipe(res);

Memory usage drops
Latency drops
Throughput increases.

🚀 7. Use process.nextTick() Only for Tiny Tasks

nextTick() jumps the queue — dangerous if misused.

Get Karuna’s stories in your inbox

Join Medium for free to get updates from this writer.

Use for microtasks only:

process.nextTick(() => {
// tiny callback
});

Never put loops or CPU tasks inside it → event loop starvation.

🚀 8. Cache Everything You Can

Redis-level caching can multiply event-loop throughput.

Example

const cache = new Map();
function getUser(id) {
if (cache.has(id)) return cache.get(id);
const user = db.getUser(id);
cache.set(id, user);
return user;
}

Lower DB I/O = happier event loop.

🚀 9. Use AbortController to Stop Long-Running Requests

Huge network delays can block your micro-task queue.

const controller = new AbortController();
setTimeout(() => controller.abort(), 2000);
fetch(url, { signal: controller.signal });

You save resources AND event-loop cycles.

🚀 10. Log Asynchronously

Sync logging kills performance.

❌ Never

console.log(JSON.stringify(obj));

✅ Use async loggers (Pino)

const pino = require("pino")({ transport: { target: "pino-pretty" } });
pino.info({ msg: "request complete" });

Pino is 5× faster than console.log.

🧵 Bonus: The Perfect Node.js Event-Loop Monitoring Snippet

const blocked = require("blocked-at");
blocked((time, stack) => {
console.log(`Event loop blocked for ${time}ms`);
});

Catch event-loop freezes before your users do.

🎯 Final Thoughts

Use these 10 hacks and your Node.js backend will:

  • handle more requests
  • reduce latency
  • avoid CPU stalls
  • survive peak traffic
  • scale with minimal cost

This is exactly how real production-scale teams treat Node.js like a performance beast.

Thank you for being a part of the community

Before you go:

👉 Be sure to clap and follow the writer ️👏️️

👉 Follow us: X | Medium

👉 CodeToDeploy Tech Community is live on Discord — Join now!

👉 Follow our publication, CodeToDeploy

--

--