What you'll learn
Performance engineering begins with a reproducible workload and a budget. Measure latency distributions, throughput, CPU, event-loop delay, allocation, and retained memory before changing code.
By the end of this lesson, you'll be able to:
- Set measurable performance budgets
- Run controlled load tests
- Read CPU and heap profiles
- Confirm optimizations with before/after evidence
Core mental model
Node.js becomes easier when you separate the JavaScript language from the runtime and the operating-system capabilities it exposes. Use this table as a decision guide.
| Concept | What it means | Decision rule |
|---|---|---|
| Percentile | A latency threshold containing a fraction of requests | Use p95/p99 alongside median and throughput |
| CPU profile | Samples showing where execution time is spent | Capture under representative load |
| Heap snapshot | Objects and retention paths in memory | Compare steady-state snapshots after forced workload cycles |
Professional workflow
Build and verify Node.js programs from the terminal in small, observable steps.
- Define the performance investigation boundary: inputs, outputs, invariants, ownership, and expected failures.
- Design the data or message contract before choosing implementation details.
- Implement the smallest correct path with dependencies passed explicitly.
- Add validation, failure translation, cleanup, and concurrency behavior.
- Verify the boundary with realistic data and at least one adversarial case.
- Measure or observe the behavior before optimizing or extracting abstractions.
Keep the feedback loop short
Guided code lab
Mark one critical operation
Performance measures attach duration to a named application boundary without relying on noisy wall-clock logging.
import { performance } from 'node:perf_hooks';
performance.mark('summary:start');
const summary = await buildOwnerSummary(ownerId);
performance.mark('summary:end');
performance.measure('owner-summary', 'summary:start', 'summary:end');
const [measure] = performance.getEntriesByName('owner-summary').slice(-1);
metrics.histogram('owner_summary_ms', measure.duration);Production practice
Contract
Every optimization names workload, environment, baseline, target metric, change, result, and correctness guard.
Verification
Warm up, hold load constant, repeat runs, inspect percentiles, use CPU/heap evidence, and rerun the full correctness suite.
Operations
Monitor latency, errors, saturation, event-loop delay, GC, RSS, heap, pool queues, and deployment regressions.
Common failure mode
Independent workshop
Profile and improve one slow endpoint without changing its contract.
Your finished workshop must include:
- Performance budget
- Reproducible workload
- Baseline report
- CPU or heap profile
- Targeted change
- Before/after comparison
Definition of done
Recap & quick check
Key takeaways
- Measure before optimizing
- Percentiles expose tails
- Profiles identify causes
- Memory leaks are retention
- Correctness guards every optimization
Quick check
1. What should precede optimization?
2. What reveals CPU hotspots?
3. What often proves a leak?
Next: Phase Project: Real-Time Processing Service