Sub topic 2
Measuring Before Optimizing
This chapter assumes basic TypeScript types, the tsc command, and the ability to read a tsconfig.json file. You will measure a build before changing compiler settings. The goal is to identify the slow phase instead of enabling optimizations blindly.
The TypeScript build pipeline
When tsc compiles a project, it performs several major steps:
- Parse: reads TypeScript and JavaScript source files and creates syntax trees.
- Bind: connects declarations, such as variables and functions, to names in their scopes.
- Check: compares types, resolves symbols, and reports type errors.
- Emit: writes JavaScript, declaration files, source maps, and related output.
A build can spend most of its time checking types, or it can spend time reading many files or writing output. The right optimization depends on the slow phase.
A fast emit step does not help much if type checking consumes almost all of the build time. Likewise, changing type annotations will not fix a project that spends most of its time processing thousands of input files.
Capture a baseline
Run the compiler with --extendedDiagnostics before making changes. Use the same command and environment for every comparison.
tsc -p . --extendedDiagnosticsThe output includes counts and timings for the current project. Typical lines include Files, Types, Instantiations, Parse time, Bind time, Check time, Emit time, and Total time.
Record at least the total time and the individual phase times. Run the command more than once because the operating system and file system can affect the first result.
| Measurement | What it indicates |
|---|---|
| Files | How much source the compiler reads |
| Types and instantiations | How much type information the checker creates |
| Parse and bind time | Cost of reading and organizing source |
| Check time | Cost of resolving and validating types |
| Emit time | Cost of writing generated files |
| Total time | End-to-end compiler duration |
The Types and Instantiations values are not durations. They are useful clues. A large number of type instantiations can point to complex generic types, while a high file count can point to an overly broad include pattern or an unwanted dependency.
Compare a clean and repeated build
For a meaningful baseline, distinguish between a clean build and a repeated build. A clean build starts without previous compiler state or output. A repeated build runs after the project has already been compiled.
At this stage, run the same command twice:
tsc -p . --extendedDiagnostics
# Run it again without changing source files.
tsc -p . --extendedDiagnosticsIf incremental compilation is not enabled, both runs usually perform nearly the same work. After enabling incremental compilation in a later chapter, the second run can reuse information from the first run.
A useful experiment changes one variable at a time:
- Record two baseline runs.
- Change one compiler option.
- Run the same command twice.
- Compare phase timings and output correctness.
- Keep the change only if it improves the relevant workload.
Interpret the results
Use the measurements to choose the next investigation:
- High parse time may indicate too many files or expensive source processing.
- High bind time may indicate a large symbol graph.
- High check time often points to complex types, broad dependencies, or repeated checking across package boundaries.
- High emit time may come from JavaScript, declaration, or source-map generation.
- A high total time with low compiler phase times may include startup or environment overhead.
The diagnostics do not explain every cause. They provide a map of where to investigate. Keep the baseline in a note or CI artifact so future changes have a reference point.
Exercise
A project reports 2 seconds of parse time, 18 seconds of check time, and 1 second of emit time. Which phase should you investigate first?
Show answer
Investigate type checking first because it dominates the measured compiler time. Inspect complex types, dependencies, and project structure before changing emit settings.