Sub topic 11
When only the editor is slow: tsserver logs
This chapter assumes you have read the earlier chapters on the phases of a tsc run, --extendedDiagnostics, and program scope. You should be able to open VS Code (or another editor that uses tsserver) and edit a tsconfig.json. The examples use VS Code settings, but the log format comes from tsserver and is the same in other editors.
Why the editor can be slow when tsc is fast
Sometimes npx tsc --noEmit finishes in a few seconds, yet completions take a second to appear, hovers lag, and red squiggles show up late. The command line and the editor run the same compiler, but their workloads differ:
tscchecks every file in the program once and exits.- The editor keeps a long-lived process alive and asks it many small questions. Each keystroke can invalidate part of the program and trigger new work.
- The editor builds its program from the project it assigns to each open file, which may not be the one you would pick.
- The editor also does work
tscnever does: completions, quick info, find-all-references, auto-imports, rename, and file watching.
So a fast tsc run does not clear the type checker. It tells you the whole-program check is cheap. The slowness may be in a single request type, a wrongly chosen project, or a process short on memory.
What tsserver is
tsserver is a Node process that wraps the compiler's language service. The editor starts it and talks to it by sending JSON requests over standard input and reading responses and events from standard output. Requests include completionInfo, quickinfo, references, and geterr, which asks for diagnostics.
Two details matter for diagnosis:
- Each file belongs to a project. If a file is covered by a
tsconfig.json, it belongs to a configured project built from that config. If not, it falls into an inferred project with default compiler options. - Before answering,
tsservermust bring the project's program up to date. After an edit or a file-watcher event, this step can cost more than the query.
Getting a tsserver log
In VS Code, turn on logging in settings.json:
{
"typescript.tsserver.log": "verbose"
}Then run the command TypeScript: Restart TS Server, reproduce the slow action, and run TypeScript: Open TS Server log. The log is also written to a file, and the command shows its location. Other accepted values are off, terse, and normal. Use verbose for diagnosis, because it includes request timings and project loading details.
Outside VS Code, you can set an environment variable before starting the editor, or pass flags when you start tsserver yourself:
# Environment variable read by tsserver at startup
export TSS_LOG="-level verbose -file /tmp/tsserver.log"
# Or start tsserver directly with the equivalent flags
npx tsserver --logVerbosity verbose --logFile /tmp/tsserver.logThe second command starts a server that waits for JSON on standard input, so it is only useful when you are driving it from a script or an editor integration.
Reading the log
A verbose log is long. You do not read it from the top. You search for timing entries and project events. The exact wording varies between TypeScript versions, so treat the strings below as search terms, not a stable format.
Useful things to look for:
- Project updates. Lines containing
updateGraphWorkermark the start and end of rebuilding a project's file graph. The finishing line reportsElapsed:in milliseconds and whether the structure was reused. Large values here mean the program is being rebuilt or re-read often. - Request timings. Lines starting with
Perfor containingelapsed time (in milliseconds)report how long a request such ascompletionInfoorquickinfotook. - Project identity. Lines naming a
tsconfig.jsonand a count of files show which project a file landed in and how many files it holds. A line mentioning an inferred project is a warning sign. - Watcher activity. Many
FileWatcherorDirectoryWatcherlines in a short time mean large numbers of file changes are being processed. - Memory restarts. A message about the server exceeding its memory limit and restarting explains a sudden pause followed by a cold start.
A quick first pass with grep shows how many events you are dealing with:
grep -c "updateGraphWorker" /tmp/tsserver.log
grep -n "Elapsed:" /tmp/tsserver.log | head -n 40
grep -n "elapsed time (in milliseconds)" /tmp/tsserver.log | head -n 40For a long log, sorting by duration is more useful. This Node script lists the slowest timing entries:
// slow-log.mjs: print the slowest timed entries in a tsserver log
import { readFileSync } from "node:fs";
const [, , file, threshold = "200"] = process.argv;
if (!file) {
console.error("usage: node slow-log.mjs <tsserver.log> [minMs]");
process.exit(1);
}
const pattern = /(?:Elapsed: |elapsed time \(in milliseconds\) )([\d.]+)/;
const hits = [];
for (const line of readFileSync(file, "utf8").split("\n")) {
const match = line.match(pattern);
if (match && Number(match[1]) >= Number(threshold)) {
hits.push({ ms: Number(match[1]), text: line.slice(0, 160) });
}
}
hits.sort((a, b) => b.ms - a.ms);
for (const hit of hits.slice(0, 20)) {
console.log(`${hit.ms.toFixed(0)} ms ${hit.text}`);
}Run it with node slow-log.mjs /tmp/tsserver.log 300 to see everything that took 300 ms or more. Entries that repeat many times with moderate values often matter more than one large outlier. A 400 ms update that happens on every keystroke is worse than a 5 s project load that happens once.
Matching the symptom to a cause
Once you know which kind of entry is slow, the likely causes narrow quickly.
| What the log shows | Likely cause | First thing to try |
|---|---|---|
| One request type is slow (for example `completionInfo`), other requests are fast | An expensive type at that position, or a huge union or auto-import candidate list | Reproduce with a trace from `tsc` on the same project, then rewrite the type |
| `updateGraphWorker` is slow and runs often | Large program, or frequent file changes invalidating it | Narrow `include` and `types`, enable project references |
| File sits in an inferred project | The file is not covered by any `tsconfig.json` `include` | Add the file's folder to `include`, or create a config for it |
| Project has far more files than `--listFilesOnly` reported | Editor loaded a different config than the one you measured | Check which `tsconfig.json` the log names |
| Many watcher events | `node_modules` changes, build output inside the watched tree, or a generated folder | Move output out of the source tree, adjust watch options |
| Out-of-memory restart messages | Program too large for the default memory limit | Reduce program scope first, then raise `typescript.tsserver.maxTsServerMemory` |
Wrong project
This is the most common surprise. The log line that names the project for a file tells you which configuration the editor used. Check these cases:
- A test file lives outside
include, so it falls into an inferred project with default options and loads many global type packages. - The repository has a "solution"
tsconfig.jsonwith"files": []andreferences. The editor must find the referenced project that owns the file. If it picks the wrong one, or loads several, startup is slow. - A file belongs to two projects, and the editor keeps a program for each.
When you find a mismatch, fix the configuration rather than the editor. Add the missing folder to the right include, or give that folder its own tsconfig.json. Then restart the server and read the log again to confirm the file now reports the project you intended.
A slow single request
If only one request type is slow, the cost is in the checker, not the project setup. The log gives the file and position. You can reproduce the same checker work from the command line. Run tsc --generateTrace on the project, as covered in the trace chapters, and look for an expensive check on that file or type. The editor adds work on top, though. Completions at a position need the checker to resolve the type there and then build the list of members and importable names, so a large union that is cheap to check may still be slow to list.
Memory
tsserver is a Node process with a heap limit. A very large program can push it near that limit, and garbage collection pauses then show up as lag across all requests. VS Code can restart the server when it passes the limit, which looks like a freeze followed by a cold start with empty caches. Reducing program scope is the better fix, since a smaller program is both faster and lighter. Raising the limit only moves the problem:
{
"typescript.tsserver.maxTsServerMemory": 4096
}The value is in megabytes.
A diagnosis workflow
Use the logs together with the earlier measurements, not instead of them.
- Confirm the symptom. Is
tsc --noEmitfast while the editor is slow? If both are slow, go back to the baseline, scope, and trace chapters. The editor is not the problem. - Enable verbose logging, restart the server, and reproduce one slow action. Keep the session short so the log stays readable.
- Find the project the slow file belongs to, and compare its file count with your
--listFilesOnlyresult. - Run the slow-entry script and decide whether the cost is project updates, one request type, or memory.
- Apply the fix that matches: configuration for scope and project problems, type rewrites for checker hot spots.
- Restart the server, repeat the same action, and compare the new timings with the old ones.
Common mistakes
- Reading the whole log. The file can hold hundreds of thousands of lines. Search for timings first.
- Measuring with logging on. Verbose logging slows the server. Use the log to find which step is slow, then confirm the improvement with logging off or with the same logging level on both runs.
- Using the wrong TypeScript version. VS Code can use its bundled TypeScript or the workspace version. The command TypeScript: Select TypeScript Version shows which one runs. If you measured with
npx tscfromnode_modulesbut the editor uses the bundled version, the two may behave differently. - Blaming the checker for a project problem. An inferred project with default options can be slow even when your types are fine.
Exercise
A developer finds that npx tsc --noEmit takes 8 seconds, but in the editor, opening test/api.test.ts takes over 20 seconds to show errors. The tsconfig.json has "include": ["src"]. The tsserver log has a line saying the file was opened in an inferred project. What is the likely cause, what would you change, and how would you confirm the fix?
Show answer
The test file is outside include, so no configured project covers it and the editor puts it in an inferred project with default compiler options. That project may load every global type package under node_modules/@types and ignore the settings you tuned. Add test to include (or give test its own tsconfig.json that extends the main one and sets types), then run TypeScript: Restart TS Server and reopen the file. Confirm in a verbose log that the file now reports the configured project and that the project update and diagnostics timings are lower than before.
Exercise
Your slow-entry script shows 40 updateGraphWorker entries of about 600 ms each in a two-minute session, and one 3 s completionInfo entry. Which should you investigate first, and why?
Show answer
Start with the repeated project updates. They total about 24 s, far more than the single 3 s request, and they happen during normal editing, so they delay every action that follows. Look at why the program is rebuilt so often: too many files in the project, watcher events from a build output folder, or a missing project reference boundary. The single slow completion is worth a trace later, but it affects one action.