@ogygia/devtools · Profiler
Slow patterns
The known slow habits of server code that the profiler recognizes on your costliest lines, each with its numbers, every place it happens and a before-and-after fix. The forecast says what a render takes once they're all fixed.
A CPU profile says which function was hot. It doesn’t say why, or what to write instead. The profiler goes one step further: it reads your source on the lines that cost the most, recognizes known slow habits there, and backs each with what was measured on that line.
Each habit becomes a card. The card says what’s happening, gives the numbers behind it, shows every place it happens (the surrounding code, with the costly line highlighted), and gives the fix as a small before and after. The same habit in several files is one card. The card’s header says what fixing all of it is worth against the median render: “about 326 ms off the 741 ms render (44%)“.
// before: a formatter built on every call (the "formatter per call" card)
export const price = (n: number) =>
new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
}).format(n);
// after: built once, used on every call
const usd = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD'
});
export const price_fast = (n: number) => usd.format(n);Patterns
The kind is the pattern’s code in the JSON (patterns[].kind).
| Pattern | Kind | What it means |
|---|---|---|
| formatter per call | formatter-per-call | new Intl.NumberFormat(…), or a locale method given a locale, on every call. Building a formatter is slow; using one is fast. |
| waits in a row | waits-in-a-row | Calls from one line, each started after the one before it ended (a batch already started together isn’t part of it). The saving is their sum minus the longest, cut to what really comes off the render when another call ran beside them. |
| same request again | repeat-request | The exact same read (method and URL) made more than once in one render. |
| same answer every render | same-answer | A GET that ran in every render and got the same bytes back each time: data to cache across requests. One row per kind of request and the line that makes it (“product/:id, 16 URLs”). A request that carried a cookie or an authorization header is marked per user, so cache it keyed by the session. The saving counts only what comes off the render. |
| same answer but for a stamp | almost-same-answer | A GET that ran in every render and never answered the same bytes twice, but whose first and last answers differ in one small stretch: at most 2% of the answer (48 bytes or more is always allowed). Usually a generation time or a request id ("generatedAt":"…"), which the card quotes. Cache it like an unchanging answer, with that part filled in or left out. |
| same work every request | same-every-request | A const line in a load whose value reads only the module (its imports, top-level constants, pure globals, or other such lines), so every request computes the same result (const valid = validateAll(PRODUCTS)). Compute it once at module level. A line that awaits or reads the load’s arguments, a let, a module new Map() or the clock never counts. |
| renders per item | render-per-item | A component rendered 20 or more times in one render, costing 5 ms or more, traced to the {#each} that renders it, the page.data key the loop walks and the load line that makes the list. Send fewer items, or make each cheaper. No saving is claimed, since it depends on how many you cut. |
| waits for a late island | late-island-wait | A call whose every page.data key the markup uses only as props of an island that wakes late (visible, idle, interaction, not already render: 'deferred'). Every request waits for it before the first byte. Move the call into the island and import it with { render: 'deferred', fetch: … }. |
| one slow call in a batch | batch-straggler | Calls started together from one line (a Promise.all) where the slowest takes 3× the next and 20 ms more. The saving is up to the gap. Start it earlier, hand its promise to what needs it, or give it a timeout with a fallback. |
| same page every request | same-document | Every render returned the same document byte for byte, no load reads cookies, locals, the raw request or the whole event, and no call carried a cookie or an authorization header. A cache can answer instead of a render (Cache-Control: s-maxage at a CDN, or postrender). A handle that personalises the page from cookies is invisible to this check. |
| same page but for a few parts | almost-same-document | The renders’ documents differ in at most 5% of their pieces and at most 6 groups, and nothing read belongs to the visitor. Each difference is named: an island’s props, one attribute stamped per render (data-track, a renderer’s ids), numbered comment markers, or text in a tag. Make those stop changing and the page caches whole. |
| library call per item | library-per-item | A library called once per item from a loop, with the library’s own time (even what it runs after an await) charged to your line. |
| rescan per item | rescan-in-loop | A big string scanned and rebuilt once per item (html = html.replace(…) in a loop). |
| search per item | lookup-in-loop | find, filter or indexOf over a list, inside a loop over another. |
| walks to find a key | scan-for-key | Object.entries(table) or Object.keys(table) walked to find one key. |
| rebuilt each call | table-per-call | A fixed table (a literal-sized Array.from, a literal new Map([…])) built inside a function that runs many times. |
| regex per call | regexp-per-call | A new RegExp(…) built on every call, often with its text escaped first. Build it once per request. |
| slow regex | slow-regex | The time is inside a regular expression itself (a RegExp: frame, charged to the line that runs it), or a pattern on a slow line can backtrack: a repeated group with nothing fixed inside, like (\w+\s?)*. A cheap pattern run thousands of times stays quiet. |
| copy per step | spread-accumulate | [...acc, x] or { ...acc, k: v } in a loop or a reduce. |
| string growth | string-build | A big string grown piece by piece in a loop. |
| sort per call | sort-per-call | A list sorted on every call. |
| date parsing | date-parse | Dates parsed from text over and over. |
| deep copy | deep-copy | JSON.parse(JSON.stringify(x)) or structuredClone. |
| blocking disk | sync-io | The render waits on the disk (readFileSync and friends) on a hot line. |
| yields per item | yield-per-item | A loop that waits for the event loop on every turn (an await per item that needs none). |
| cache never hits | cache-never-hits | A memo (M.get(k), an early return, M.set(k, …)) whose miss path ran about as often as the function. Measured from V8’s call counts, which survive inlining; the usual cause is a key that’s a fresh object every call. |
| kept after render | kept-per-render | Memory still alive after a warm render (Retained memory). |
| ships all page data | seed-whole-read | An island that takes page.data whole (Oversized island data). |
Recognition
A pattern is reported only where the profile actually paid for it: the line has its own CPU time or allocations, the code has the right shape, and the context agrees (the line sits in a loop, its function ran more than once, or a caller runs it from a loop). A pattern that would save under a millisecond isn’t reported. The code is read with a small tokenizer, not regular expressions, and only for the lines the report names.
Optimized code charges an inlined function’s time to the line that calls it, and a loop body’s time to the loop header. When such a line has no shape of its own, the recognizer reads one level in, into the called function or the loop’s block, and names the line it finds there.
A whole function inlined into its caller can also land its time on a neighbouring line, and V8’s profile doesn’t say which. So the render that counts calls (which runs with inlining off) is sampled too, and in it every function keeps its own frame. When a line’s neighbour calls a function that has no frame in the main profile, the line’s time is split by that render: the line keeps its own work, and each neighbour gets its inlined function’s share. The line list marks this as time moved from line N. The un-inlined render decides only the split, never the amount, because everything runs slower without inlining. Only the two lines above and the one below are searched, and a function name defined in more than one file isn’t moved.
CPU and waiting are both per render. A page profile’s CPU adds up every run, so it’s divided by the number of runs.
Forecast
Above the cards, the forecast says what one render would take with every fix done (“about 514 ms → 163 ms”). It doesn’t add the fixes up naively, because many of them are the same time counted twice:
- Fixes on the same line count once. A formatter built per call is often also “a library per item” on that line, and one slow call is often both “waits in a row” and “one slow call in a batch”. Such a group gives back its biggest saving, and the forecast names which fix covers which.
- Separate fixes add up, for example two chains of waits that run one after the other, or a loop in one file and a parse in another.
- Fixes inside a call that another fix moves count once. Moving
attachBrands(valid)out of the request takes the slow search inside it along, so the search’s own fix adds nothing more. The call stacks show which work runs only inside a moved call. - Work for data nothing reads is deleted. Its time counts, and any slow pattern inside it is left out, because deleting the work beats speeding it up.
- Waiting is capped at what the render really waited, and CPU at what it really used. If the fixes claim more than that, they overlap in a way the lines don’t show, and the forecast says so.
Keeping answers is a second figure. “The same answer on every render” is a fix only where an answer may be a little stale. That’s a decision about freshness, so the forecast shows it apart: “keeping the services’ answers too: about 51 ms”. It’s measured, not guessed: after the timed renders, the profiler renders the page up to three more times with those same answers served from memory (only its own renders get them), and the figure is that render with the other fixes taken off. A cached copy of the whole page is left out for the same reason.
Capacity is a third line. A waiting fix shortens how long a visitor waits, but only a CPU fix lets one core serve more requests. So the forecast also gives the main-thread time per render now and after the CPU fixes, as renders per second per core (“about 4 → 32”). That counts your code, packages, Node and the collector’s pauses on the main thread, not the collector’s helper threads. On AWS Lambda, which bills memory times the whole render including waits, it also gives the GB-seconds per million renders now and after.
Pick the fixes you’ll make. Under the forecast is a checklist of its parts. Untick what you won’t do, and the page recounts the render after the rest; a fix another one covers comes along with it. That answers “is the waiting fix alone enough?” without a second profile.
The forecast is checked against real fixes. Three test pages (one mostly waiting, one mixed, one mostly CPU) each have a copy with every counted fix applied by hand, and each forecast lands within a few percent of what the fixed copy measures: 33.4 ms forecast against 34.3 measured, 169 against 155, and 397 against 384, both sides on a freshly started server. A build without source maps shows fewer lines and so fewer fixes, and the forecast says so (Builds without source maps).
After you fix things, a comparison checks whether the render got as much faster as the forecast said.
Hot lines
This list shows your own lines that cost the most (up to 40), one row each, with the code and every cost joined onto the line, per render: CPU (the line’s own plus the time inside the libraries it called), bytes allocated, the GC they caused, and bytes kept alive.
A CPU profile never records where a library was called from, so the profiler finds the line by reading the calling function’s source for the library function’s name or its import alias. Memory is measured per function rather than per line, so a function’s memory is shown on its busiest line, and the row says so. A line inside a dependency isn’t listed, because its cost reaches the list through the line of yours that called it.
Unused data
Rows of the line-level breakdown whose page.data keys nothing reads add up to one number: “newestReview 55 ms (CPU) → freshest, GET api/live 16 ms (waiting) → live_at, 71 ms of one render for nothing”. It joins the first fixes when it’s among the biggest, and the key-unread finding says the same. A slow pattern inside that work is left out there, because deleting the work beats speeding it up. Data lineage explains how readers are found.
Paths to fix
After you remove the one function that took two seconds, a dozen smaller ones usually remain, and often several of them sit on one call path. If walk, format and sortRows all run under buildTree, fixing buildTree once (calling it less, caching what it computes, or moving it out of the render) takes all three with it.
The report finds these groups by crediting each hot function’s time to every one of your callers above it. A caller that covers two or more hot functions is a path, and the deepest caller that covers the time wins, so the group names the most specific place to fix and never the route’s root. The path-group finding names the top three, as a warning when a path holds 15% or more of busy time and 3 ms or more per render.
Each group is drawn as a graph: the caller to fix on the left, the chain of calls through the middle, and the hot functions at the end. A link’s width is the time that flowed through it, and clicking a node opens its row. A node named fn @ file:line is an anonymous function (an arrow or a callback) that V8 can’t name. When the source is on the machine, the label carries the line itself (fn @ ds-ssr.ts:42 · tags.map(async (tag) => {).