Performance के Tips
Event loop free रखना और काम reuse करना Node apps को fast बनाता है।
In this page:
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
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. #}
आम गलतियां
- sync code से loop block करना
- बड़ी files पूरी तरह memory में load करना
- बिना 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: