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.log β†’ uncontrolled 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)