DEV Community

Alex Aslam
Alex Aslam

Posted on

The Hidden Cost of console.log() in Production: How We Lost 40% Performance

The Mystery of the Slow API

Our monitoring dashboard showed something strange: 40% slower response times during peak hours. The odd part? CPU and memory looked fine.

After days of profiling, we discovered the culprit:

console.log() statements left in production.

Here’s why they’re dangerous—and how to fix them.


1. Why console.log() is Worse Than You Think

🚨 Problem #1: It’s Synchronous (Yes, Really!)

console.log('Logging data:', largeObject); // Blocks the event loop!
Enter fullscreen mode Exit fullscreen mode

Proof:

console.time('sync');
for (let i = 0; i < 1e5; i++) console.log(i); // ~3 seconds!
console.timeEnd('sync');
Enter fullscreen mode Exit fullscreen mode

🚨 Problem #2: Memory Leaks in Logging Libraries

Some logging frameworks buffer logs in memory before writing to disk/network.

  • Example: Unstructured console.loguncontrolled growth in long-running processes.

🚨 Problem #3: Security Risks

console.log('User token:', req.headers.authorization); // 😱 Logs secrets!
Enter fullscreen mode Exit fullscreen mode

2. How We Fixed It

✅ Switch to Async Logging (Winston/Pino)

// Before (Dangerous)
console.log(`User ${userId} logged in`);

// After (Safe)
const logger = require('pino')();
logger.info({ userId }, 'User logged in'); // Async + structured
Enter fullscreen mode Exit fullscreen mode

Performance Boost:
| Logger | Req/sec | Latency (p99) |
|--------------|---------|--------------|
| console.log | 8,000 | 120ms |
| Pino | 14,000 | 45ms |

✅ Use Log Levels (Avoid Spam in Prod)

if (process.env.NODE_ENV === 'development') {
  console.log('Debug info'); // Only in dev
}
Enter fullscreen mode Exit fullscreen mode

✅ Automatically Strip Logs in Production

# Use Babel/ESBuild to remove console.* in prod
esbuild app.js --drop:console
Enter fullscreen mode Exit fullscreen mode

3. When console.log is Actually Okay

CLI tools (short-lived processes)
Early prototyping (before proper logging is set up)
Browser debugging (but use debugger instead)


Key Takeaways

🔇 console.log blocks the event loop (yes, even in Node.js!)
📉 Uncontrolled logging tanks performance (we lost 40% throughput)
🔒 Logs can leak secrets (check for sensitive data)
🚀 Use Pino/Winston for async, structured logging


Further Reading


Have you been burned by logging in prod? Share your story!

Top comments (0)