Summary - Performance
- Node.js course
- Advanced
- Lesson 18/83
- 5 min
Combine perf_hooks, memoryUsage and profiling to write a benchmark tool.
Summary - Performance
In this group you learned performance tools: time measurement with perf_hooks, memory monitoring with process.memoryUsage(), CPU profiling with inspector. Event loop latency is an early warning for bottlenecks.
The optimization order: measure, find the bottleneck, remove it, measure again. Move CPU work to a worker thread, make I/O asynchronous, cache repeated calculations. Avoid premature optimization; always rely on data.
Performance checklist
Performance checklist: (1) Event loop latency should be under 10 ms. (2) heapUsed should grow steadily, without leaks. (3) CPU profiles should show the bottleneck function. (4) Response times should be consistent.
In production, APM tools (New Relic, Datadog) collect these metrics automatically. Adding your own measurements is a good habit; but use existing tools first.
Practice Check if now is a function
Import performance from perf_hooks. Log typeof performance.now === "function".
Starter code
// import the module, get the timer
// log if now is a functionSample solution
const { performance } = require("perf_hooks");
console.log(typeof performance.now === "function");Expected output
true
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the first step before optimization?
- Kodu yeniden yazmak
- Measuring (profiling) Correct answer
- Installing a new module
- Switching servers
Explanation: Measure first, then improve.
2 Match each piece to its role
| Term | Meaning |
|---|---|
perf_hooks |
Measures time |
process.memoryUsage() |
Gives memory |
inspector |
CPU profile |
event loop latency |
Measures delay |