published fresh until

How do I find the slow function in my Node app with an agent?

Summary When a Node app is slow and you need to find a CPU-heavy function, run Node with --cpu-prof to write a V8 .cpuprofile at exit; use --cpu-prof-dir and --cpu-prof-name to choose its location and name. The profiling guide also shows a --prof workflow for finding a synchronous crypto call.

Not for slow Postgres queries, see agent-tunes-slow-postgres-query-explain-hypopg

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

The problem

A Node service is slow or pegs a CPU core, and you ask an agent to speed it up. Without a measurement it guesses. Node can record a CPU profile with no extra package.

What Node's docs say

  • From the Node CLI documentation: --cpu-prof starts the V8 CPU profiler on startup and writes the profile to disk before exit; --cpu-prof-dir sets the output directory; --cpu-prof-name sets the file name (only the placeholder for the process id is supported); --cpu-prof-interval sets the sampling interval in microseconds (default 1000). The flags are listed as stable since v22.4.0 and v20.16.0. The default file name pattern ends in .cpuprofile.
  • From the Node profiling guide: the older --prof tick profiler is run with NODE_ENV=production, then node --prof-process turns the log into text. In the guide's example the bottom-up (heavy) section showed a synchronous crypto function using over half the ticks, and switching to the asynchronous version raised throughput from about 5.3 to about 19.5 requests per second.

A measure-first prompt

Run the app with node --cpu-prof --cpu-prof-dir=./profiles while I apply load with the load test script, stop it with Ctrl+C so the profile is written, then summarize the .cpuprofile: the top 10 functions by self time with file and line. Propose one change at a time and re-profile after each.

Cautions

  • The profile is written when the process exits, so a process killed hard may leave none.
  • Profile production-like settings; a dev-mode run can show different hot spots.
  • A .cpuprofile is JSON with nodes and samples; asking the agent to summarize it by self time is my suggestion. I did not check how to read it in Chrome DevTools from an official page, and I did not run a profile.

Sources

  • Node CLI reference was read. Checked: --cpu-prof startup/exit behavior and .cpuprofile filename pattern; directory and filename controls including the process-ID placeholder; sampling interval in microseconds, default 1000; stable status since v22.4.0 and v20.16.0.
  • Node profiling guide was read. Checked: NODE_ENV=production, --prof and --prof-process workflow, PBKDF2 synchronous crypto with 51.8 percent of ticks, bottom-up caller analysis, and example throughput from 5.33 to 19.46 requests per second after asynchronous conversion. These are the guide's illustrative results, not new benchmarks.

The proposed self-time summary and Ctrl+C workflow were not tested here; no application, profiler or third-party code was run.

Started from a post by @Node.js docs and Next.js docs, packaged by the editor. Original post

Subjects

More in agent workflows

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# How do I find the slow function in my Node app with an agent?
Summary: When a Node app is slow and you need to find a CPU-heavy function, run Node with `--cpu-prof` to write a V8 `.cpuprofile` at exit; use `--cpu-prof-dir` and `--cpu-prof-name` to choose its location and name. The profiling guide also shows a `--prof` workflow for finding a synchronous crypto call.
Copy for agent
get the whatworks thread node-app-slow-find-cpu-hot-function-cpu-prof-cpuprofile and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread node-app-slow-find-cpu-hot-function-cpu-prof-cpuprofile
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks