Working with Performance Hooks

1. Measuring Performance (performance.now)

Example

import { performance } from "node:perf_hooks";
const t0 = performance.now();
work();
console.log("elapsed ms:", performance.now() - t0);

2. Creating Performance Marks (performance.mark)

APIUse
performance.mark("start")Named timestamp
performance.clearMarks(name?)Remove

3. Measuring Between Marks (performance.measure)

Example

performance.mark("a");
work();
performance.mark("b");
performance.measure("work", "a", "b");

4. Using Performance Observer (PerformanceObserver)

Example

import { PerformanceObserver } from "node:perf_hooks";
const obs = new PerformanceObserver((items) => {
  for (const e of items.getEntries()) console.log(e.name, e.duration);
});
obs.observe({ entryTypes: ["measure", "function"] });
entryTypeSource
mark / measureUser Timing API
functionWrapped via performance.timerify
http / http2Network entries
gcGarbage collection
dnsLookups

5. Tracking HTTP Timing

PhaseField
DNSreq.socket.lookupTime (estimate)
TCP connectSocket "connect" event
TLS handshakeSocket "secureConnect"
TTFBTime until response headers

6. Monitoring Event Loop Lag

APIUse
monitorEventLoopDelay()Histogram (min/max/percentile)
performance.eventLoopUtilization()Idle vs active ratio

7. Using perf_hooks for Profiling

Example: timerify

import { performance } from "node:perf_hooks";
const wrapped = performance.timerify(slowFn);
wrapped(input);

8. Measuring Function Execution Time

MethodUse
console.time / timeEnd("label")Quick measurement
performance.timerifyAuto-record entries
Benchmark.js / mitata / tinybenchStatistical benchmarks

9. Creating Custom Metrics

LibraryUse
prom-clientPrometheus exposition format
OpenTelemetry SDKVendor-neutral metrics + traces

10. Analyzing Performance Data

ToolUse
Chrome DevTools PerformanceFlamecharts
0xCLI flamegraph
Clinic.js suiteSymptom analysis
Grafana / PrometheusLong-term trends