--- name: perf-profile description: Profiles a slow function, endpoint, script or test using a profiler suited to the stack, identifies hotspots from the profile data, applies one change at a time, and reports before and after measurements from the same benchmark. Use when the user says something is slow, asks to optimize performance, reduce latency, CPU or memory, or to profile code. --- # Performance profiling Target: `$ARGUMENTS` Rule: no optimization without a measurement before and after. Store artifacts in `.perf/`. ## 1. Define and benchmark - Restate the target as a measurable thing: "median wall time of X with input Y". - Find or write the smallest repeatable benchmark: an existing benchmark, a test, a script that calls the function with realistic input, or a local HTTP request loop. Use local or test environments only. - Run it at least 5 times after one warm-up run. Record median and spread. If spread is large, fix the benchmark first. ## 2. Profile Pick the tool for the stack: - **Node.js**: `node --cpu-prof --cpu-prof-dir=.perf