Sub topic 2
Measure first: reading --extendedDiagnostics
This chapter shows how to take a baseline measurement of a TypeScript compile and how to read the numbers. It assumes you can run tsc from a project directory, that your project has a tsconfig.json, and that you know that tsc parses, checks and emits files. Every later chapter compares its results against the baseline you record here.
Why a baseline comes first
Slow compiles invite guesses. People blame a large dependency, a clever generic, or the laptop. Each guess may be right, but a fix only counts if a number moved. A baseline is a set of measurements taken before you change anything. It gives you two things:
- A way to see which phase of the compiler is slow, so you look in the right place.
- A reference to compare against after each change, so you know whether the change helped.
Running the command
The flag --extendedDiagnostics makes tsc print counts and timings after it finishes. Combine it with --noEmit so that the run does only parsing, binding and checking, and so that writing files does not affect the result.
npx tsc --noEmit --extendedDiagnosticsA typical output looks like this. Your numbers will differ.
Files: 1243
Lines of Library: 38212
Lines of Definitions: 412907
Lines of TypeScript: 58304
Lines of JavaScript: 0
Lines of JSON: 0
Lines of Other: 0
Identifiers: 402118
Symbols: 551290
Types: 198443
Instantiations: 902115
Memory used: 612345K
Assignability cache size: 121004
Identity cache size: 4410
Subtype cache size: 2290
Strict subtype cache size: 810
I/O Read time: 0.21s
Parse time: 1.64s
ResolveModule time: 0.38s
ResolveTypeReference time: 0.02s
Program time: 2.51s
Bind time: 0.62s
Check time: 7.93s
printTime time: 0.00s
Emit time: 0.00s
Total time: 11.06sThe exact set of lines depends on your TypeScript version. Some versions add lines such as Config file, Strict subtype cache size or Total time formatting differences. Read the names, not the positions.
The counts
The first block of output describes the size of the program, which is the set of files tsc loaded.
| Line | What it counts |
|---|---|
| Files | Every source and declaration file in the program, including those from `node_modules` and the built-in library files. |
| Lines of Library | Lines in the default `lib.*.d.ts` files, chosen by `target` and `lib`. |
| Lines of Definitions | Lines in all other `.d.ts` files, mostly from `node_modules/@types` and packages that ship their own types. |
| Lines of TypeScript | Lines in your `.ts` and `.tsx` files. |
| Identifiers, Symbols | Names found while parsing and the symbols created for them while binding. |
| Types | Distinct type objects the checker created. |
| Instantiations | How many times a generic type or function was instantiated with specific type arguments. |
| Memory used | Peak memory estimate for the process. |
Two comparisons are worth making right away.
- Compare
Lines of DefinitionswithLines of TypeScript. In the sample, definitions are seven times larger than your own code. That ratio is common in projects with many dependencies, and it tells you the program is mostly other people's declarations. The next chapter shows how to find out why those files are included. - Compare
TypesandInstantiationswith the size of your code. A modest codebase that creates millions of instantiations is doing heavy type-level work, and a trace (covered later) will show where.
The timings
The second block reports how long each phase took. These map to the phases from the previous chapter.
| Line | Phase | What makes it large |
|---|---|---|
| I/O Read time | Reading files from disk | Slow disks, network drives, huge file counts. |
| Parse time | Parse | Many files or many lines, including declaration files. |
| ResolveModule time | Part of program creation | Many imports, deep `node_modules`, complex `paths` or `moduleResolution` settings. |
| Program time | Building the program (reading, parsing, resolving) | The sum of the file loading work above. |
| Bind time | Bind | Number of symbols. Rarely the problem. |
| Check time | Check | Type complexity, number of files that are checked, number of instantiations. |
| Emit time | Emit | Output size. It is near zero with `--noEmit`. |
| Total time | Whole run | Roughly the sum of the phases. |
In the sample, Check time is 7.93 seconds of 11.06. That is 72 percent. When the check phase dominates, the investigation moves to what is being checked and how expensive those types are. When Program time dominates, the program probably contains too many files, and the fix lies in configuration rather than in your types.
The phases are not independent. A larger program raises both parse time and check time, because every included file that is not a declaration file skipped by skipLibCheck has to be checked. This is why scope comes before type tuning in this tutorial.
A decision table
Use the dominant phase to choose where to look next.
| What you see | Likely cause | Next step |
|---|---|---|
| `Files` is much larger than expected | Globs or imports pulling in extra files, or many `@types` packages | Inspect scope with `--listFilesOnly` and `--explainFiles`. |
| `Lines of Definitions` far exceeds `Lines of TypeScript` and `Check time` is high | Declaration files being checked | Consider `skipLibCheck`. |
| `Parse time` and `Program time` are high, `Check time` is moderate | Too many files, slow I/O, slow module resolution | Reduce scope, look at `ResolveModule time`. |
| `Check time` dominates and `Files` is reasonable | Expensive types in your code or in a library | Generate a trace. |
| `Instantiations` is very high | Heavy generics, deep conditional or mapped types | Generate a trace, then rewrite types. |
| `Memory used` is near the Node heap limit | Very large program or very large types | Reduce scope or split with project references. |
Getting a trustworthy number
A single run is a weak measurement. Several things distort it.
- Cold versus warm disk cache. The first run after a reboot or a branch switch reads files from disk. Later runs read from the operating system cache. Run the command once and discard the result, then record later runs.
- Background load. A browser, an editor's own
tsserver, or a running test suite competes for CPU. Close what you can, or accept more noise. - Incremental state. If
incrementalorcompositeis on in your config, a previous.tsbuildinfofile can make a run faster than a clean one. For a baseline, delete the build info file or measure with a clean checkout. Chapter on incremental builds covers this in depth. - Different TypeScript versions. Compare results only when
npx tsc --versionprints the same version.
A small script makes repeated runs easy and keeps the useful lines.
#!/usr/bin/env bash
set -euo pipefail
runs=${1:-5}
npx tsc --version
# Warm-up run, result discarded.
npx tsc --noEmit > /dev/null || true
for i in $(seq 1 "$runs"); do
echo "run $i"
npx tsc --noEmit --extendedDiagnostics \
| grep -E '^(Files|Lines of (Library|Definitions|TypeScript)|Types|Instantiations|Memory used|Parse time|Program time|Bind time|Check time|Total time)'
doneThe || true on the warm-up run lets the script continue when your project has type errors. A project with errors still produces timings, and errors do not stop the checker from visiting the rest of the program. Keep in mind that a codebase with many errors may spend extra time producing and printing them.
Record the median of the runs, not the best one. Write down the TypeScript version, the Node version, the machine, and the date next to the numbers. A baseline without that context cannot be reproduced.
Reading changes between runs
After you change something, run the same command and compare. Two rules help avoid false conclusions.
- Judge by the phase you targeted. If you changed a type and
Check timefell from 7.9 to 6.1 seconds whileParse timestayed the same, the change worked as intended. A total that fell because the disk cache was warm proves nothing. - Treat differences under about 5 to 10 percent as noise unless you have many runs that agree. Counts such as
InstantiationsandTypesdo not vary between runs of the same code, so they are a more stable signal than seconds. A change that lowersInstantiationsby 30 percent is real even if the timing looks the same on a noisy machine.
That placeholder is not used. The flow below shows the loop to follow.
Change one thing at a time. If you apply skipLibCheck, a new types list and an interface rewrite together, you cannot tell which one paid off, and one may have made things worse.
Common mistakes
- Measuring with emit on and comparing with emit off.
Emit timeandprintTimeare included inTotal time. Keep--noEmitconsistent. - Reading
Total timeonly. The total hides which phase changed. Always read the phase lines. - Comparing
tscwith the editor.tscreports a full program check. The editor usestsserver, which checks open files on demand and has its own costs. A fasttscrun does not rule out a slow editor, which is covered in a later chapter. - Ignoring memory. A run that nearly exhausts the heap slows down because of garbage collection, and it may crash with an out-of-memory error. If
Memory usedis in the gigabytes, treat it as a problem in its own right. - Using
--diagnosticsand expecting the same output.--diagnosticsprints a shorter report.--extendedDiagnosticsadds the cache sizes and the finer timing lines, so use the extended form for a baseline.
Worked example
Suppose a project reports these baseline numbers from the median of five runs.
| Metric | Value |
|---|---|
| Files | 1243 |
| Lines of Definitions | 412907 |
| Lines of TypeScript | 58304 |
| Program time | 2.51s |
| Check time | 7.93s |
| Total time | 11.06s |
The reading goes like this. Check time is the largest phase, at about 72 percent. Definitions outnumber your code seven to one, so a large share of that check work may be going into declaration files, and 1243 files for a 58,000-line codebase is more than expected. Both points sit under program scope, so the next step is --listFilesOnly and --explainFiles, not a trace. If scope turns out to be reasonable and Check time stays high after the scope fixes, the trace is the next tool.
Summary of what to record
Before leaving this chapter, write down these values for your own project:
- TypeScript version and Node version.
Files,Lines of Definitions,Lines of TypeScript.TypesandInstantiations.Memory used.Parse time,Program time,Bind time,Check time,Total time, as medians.
Exercise
A run prints Files: 4100, Lines of Definitions: 1,900,000, Lines of TypeScript: 70,000, Parse time: 6.2s, Program time: 8.0s, Check time: 9.5s, Total time: 18.4s. Which two lines point toward investigating program scope before looking at individual types, and why?
Show answer
Files (4100) and Lines of Definitions (1.9 million, about 27 times Lines of TypeScript) show that the program is dominated by declaration files. Program time of 8.0s (about 43 percent of the total) confirms that loading files is a large cost. Check the included files with --listFilesOnly and --explainFiles before tracing types.
Exercise
You change one type and Total time drops from 11.1s to 10.4s on a busy laptop, but Instantiations is unchanged. Did the change help?
Show answer
Probably not. A 6 percent drop is within normal noise, and unchanged Instantiations (a count that is stable between runs) suggests the checker did the same work. Repeat the measurement several times and compare medians, and look at Check time rather than Total time.