← Back to Node.js Course | Chapter 10: Deployment & Best Practices | Lesson 7 of 7

Performance के Tips

Event loop free रखना और काम reuse करना Node apps को fast बनाता है।

In this page:

  1. Performance Tips

Performance Tips

sync calls या heavy computation से event loop block करने से बचें; CPU work के लिए worker threads इस्तेमाल करें। Repeated results cache करें, बड़े data के लिए streams इस्तेमाल करें, compression enable करें, connections reuse करें, और optimize करने से पहले node --prof या inspector से profile करें।

Note: पहले measure करें: bottlenecks guess करना time waste करता है।

उदाहरण: Performance tips

javascript
const { performance } = require("perf_hooks");
const cache = new Map();
function slowSquare(n) { let x = 0; for (let i = 0; i < 1e6; i++) x = n * n; return x; }
function cached(n) { if (!cache.has(n)) cache.set(n, slowSquare(n)); return cache.get(n); }
cached(9);
const t0 = performance.now(); cached(9); const hit = performance.now() - t0;
const t1 = performance.now(); slowSquare(9); const miss = performance.now() - t1;
console.log("cache hit faster than recompute:", hit < miss);

// Output:
// cache hit faster than recompute: true

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. sync code से loop block करना
  2. बड़ी files पूरी तरह memory में load करना
  3. बिना profiling के optimize करना
चैप्टर सारांश
  • Event loop free रखें
  • CPU work के लिए worker threads इस्तेमाल करें
  • Cache और stream करें
  • Optimize करने से पहले profile करें
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.