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)