CodePark/ questions / Performance & memory leaks
START PRACTICINGPRACTICE
Backend question · Node.js › Performance & memory leaks

An endpoint has high CPU usage and slow responses. How would you profile it in Node?

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 .cpuprofile file 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.
  • --prof uses V8's tick profiler. You process its output with node --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-prof or --inspect with DevTools. --prof is 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.