# Profiling And Benchmarking

> Measure before optimizing — use CPU/memory profilers and benchmarks to find real bottlenecks instead of guessing; covers Node.js and Chrome DevTools.

- Skill: `gabriel7silva/profiling-and-benchmarking` (Agent Skill)
- Install (CLI): `npx get-skill add gabriel7silva/profiling-and-benchmarking`
- Raw SKILL.md: https://api.skillmd.com/api/skills/gabriel7silva/profiling-and-benchmarking/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: gabriel7silva (https://skillmd.com/u/gabriel7silva)
- Updated: 2026-11-24
- Page: https://skillmd.com/skills/gabriel7silva/profiling-and-benchmarking

---


# Profiling and Benchmarking

## Overview
The first rule of optimization is to measure: profilers tell you where time and memory actually go, and benchmarks confirm whether a change helped. This skill covers sampling profilers, flame graphs, and runtime/memory analysis using the built-in Node.js profiler and the Chrome DevTools Performance panel. Read it before optimizing anything, so effort lands on the real bottleneck.

## Official sources
- Node.js profiling guide: https://nodejs.org/en/learn/getting-started/profiling
- Chrome DevTools Performance panel: https://developer.chrome.com/docs/devtools/performance
- Chrome DevTools memory problems: https://developer.chrome.com/docs/devtools/memory-problems
- Node.js source (repo): https://github.com/nodejs/node

## Core concepts
- **Sampling profilers.** Node's `--prof` uses V8's built-in profiler to sample the call stack at intervals and record events as "ticks" in an `isolate-*.log`, processed with `--prof-process` into a human-readable summary.
- **Bottom-up vs summary views.** The processed tick output groups time by Summary, C++, and a bottom-up profile, letting you trace which functions and their callers dominate CPU time.
- **Flame graphs.** The DevTools Performance panel visualizes main-thread activity as a flame chart so you can spot wide (expensive) frames and long tasks.
- **Runtime vs load performance.** Per Chrome, runtime performance is how a page behaves while running (Response, Animation, Idle phases of the RAIL model), distinct from initial load.
- **Memory problem classes.** DevTools distinguishes memory leaks (steadily growing usage), memory bloat (using more than needed), and frequent garbage collection that pauses script execution.
- **Benchmark vs profile.** A benchmark measures aggregate throughput/latency of a change; a profile attributes cost within a run. Use benchmarks to decide *whether* something is faster and profiles to learn *why*.

## Best practices
- **Measure before and after, with a baseline.** Establish a repeatable benchmark first; otherwise you cannot tell whether an optimization helped or hurt.
- **Profile under production-like conditions.** Run Node profiling with `NODE_ENV=production` and realistic load, as the official guide demonstrates, so JIT and code paths match production.
- **Find the dominant cost, then optimize that.** The Node guide's example showed synchronous `pbkdf2` consuming ~51.8% of CPU; switching to the async version raised throughput from 5.33 to 19.46 req/s — fix the biggest frame first.
- **Use heap snapshots and the allocation timeline for leaks.** In DevTools, compare snapshots over time and watch for detached DOM nodes and steadily growing retained size.
- **Keep benchmarks isolated and warmed up.** Exclude setup, warm the JIT, and run enough iterations to get stable numbers before comparing.

## Common pitfalls
- **Optimizing by intuition** → developers routinely guess the wrong hotspot; always confirm with a profile before changing code.
- **Profiling a dev build or trivial workload** → results won't reflect production; profile production builds under representative load.
- **Reporting a single run as a benchmark** → variance and JIT warmup distort one-shot numbers; run multiple warmed iterations and compare distributions.
- **Confusing high GC frequency with a leak** → frequent GC may indicate excessive short-lived allocation (bloat), not retained memory; distinguish with the allocation timeline.

## Examples
```bash
# Node.js: capture a CPU profile, then turn it into a readable report
NODE_ENV=production node --prof app.js
# ...generate load against the app, then stop it
node --prof-process isolate-0xnnnnnnnnnnnn-v8.log > processed.txt
```

## Further reading
- https://developer.chrome.com/docs/devtools/memory-problems — diagnosing leaks, bloat, and GC churn
- https://nodejs.org/en/learn/getting-started/profiling — full Node.js profiling walkthrough
- ./reference.md — flame-graph reading and benchmark harness tips (loaded only when needed)

## Related skills
- ../backend-performance — what to do once a bottleneck is found server-side
- ../web-performance-core-vitals — field metrics that tell you what to profile on the front end

