First, reproduce the load, then capture a CPU profile. Run the load locally or on a staging server with a load tool such as autocannon.
The simplest ways to capture a profile:
- Start Node with
--cpu-prof. It writes a.cpuprofilefile when the process exits. You open that file in Chrome DevTools. - Start Node with
--inspect, attach DevTools, and record in the Performance or Profiler tab. --profuses V8's tick profiler. You process its output withnode --prof-process.
Read the result as a flame graph. Look for wide frames: these are functions where a lot of time is spent. Typical findings:
- Synchronous JSON work on big payloads.
- Expensive regexes.
- Synchronous crypto.
- Repeated work that should be cached.
Also watch the event loop delay with perf_hooks.monitorEventLoopDelay. This lets you connect high CPU use with slow responses.
- First, reproduce the problem under load.
- Profile with
--cpu-profor--inspectwith DevTools.--profis another option. - In the flame graph, look for wide, hot frames.
- Common causes are big JSON work, slow regexes and synchronous crypto.
- Monitor the event loop delay too.