Sub topic 2
Profile a production-like build with source maps
This chapter explains why you should profile a production-like build instead of the dev server. It then shows how to set up a build you can profile, how to serve it, and how to check that DevTools maps the minified code back to your TypeScript. It assumes you can run ng build and ng serve, know the Performance panel exists, and have read the previous chapter on measuring before optimizing.
Why the dev server misleads you
ng serve is tuned for fast rebuilds, not for speed at run time. A trace recorded against it contains work that real users never trigger.
| Aspect | Dev server (`ng serve`) | Production-like build |
|---|---|---|
| Minification and tree-shaking | Off | On |
| Angular dev-mode checks | On | Off |
| Live reload client | Present | Absent |
| Bundle size | Large | Much smaller |
| Source maps | Usually inline | Separate `.map` files, if enabled |
| Function and variable names | Original | Shortened unless you keep them |
Dev mode adds three costs that distort a trace:
- Extra checks. In dev mode, Angular runs a second pass after every change detection run to detect
ExpressionChangedAfterItHasBeenCheckederrors. A template expression can appear to run twice as often as it does in production. - Extra assertions. The framework validates inputs, template bindings, and injection tokens, and these checks are removed from production code.
- Extra code. Unminified bundles take longer to download, parse, and compile. The dev server also injects a live reload client that keeps a connection open to the server.
A function that looks hot in a dev trace may not matter in production. A real bottleneck can also be hidden by noise. So profile something close to what users receive.
Add a profiling configuration
You could profile the default production configuration, but it has drawbacks. It often disables source maps, and you may not want to change what you ship. A separate configuration keeps the two apart. It starts from the same optimizations and turns on what the profiler needs.
In angular.json, add a profiling entry under the build target's configurations. The project name my-app is a placeholder for yours.
{
"projects": {
"my-app": {
"architect": {
"build": {
"configurations": {
"profiling": {
"optimization": true,
"outputHashing": "all",
"sourceMap": {
"scripts": true,
"styles": false,
"vendor": true,
"hidden": false
},
"namedChunks": true,
"extractLicenses": false
}
}
}
}
}
}
}The options that matter here:
optimization: trueturns on minification, dead-code elimination, and the other production transforms. This is what makes the trace realistic.sourceMap.scripts: trueemits.mapfiles for your JavaScript.vendor: truealso maps Angular and library code, so you can read framework frames in the flame chart. It makes the build slower and the output larger, but only for this configuration.hidden: falsekeeps the//# sourceMappingURL=comment at the end of each bundle. Withhidden: truethe maps are written but not linked, and DevTools will not find them on its own.namedChunks: truegives lazy-loaded chunks readable file names, which helps later in the Network panel.
Build it:
ng build --configuration profilingThe output goes to dist/my-app/browser for the default application builder. Check your own outputPath if the folder differs.
Serve the output
Serve the built files with a plain static server. This avoids the dev server's live reload client and watch mode.
npx http-server dist/my-app/browser -p 8080 -c-1 --gzip-c-1 disables caching, so a rebuild is always picked up. If your app uses client-side routing, you may need a server that falls back to index.html. With http-server, add --proxy http://localhost:8080? so deep links resolve.
You can use ng serve --configuration profiling, but it still runs through the dev server. Treat the static server as the reference for any number you report.
Set up the browser
The browser can change your results as much as the build can.
- Open an Incognito window. Extensions are disabled there by default, so they add no content scripts to the page.
- Close other tabs. Background tabs compete for CPU.
- Leave the machine plugged in, and don't change anything else on it during a recording. Laptops on battery may lower clock speed.
- Open DevTools before loading the page, so the first load is also recorded if you need it.
Check that source maps work
Before you record anything, confirm that DevTools can map the minified code back to your TypeScript.
- Open the Sources panel and press
Ctrl+P(Cmd+Pon macOS). - Type a file name from your app, such as
order-total.component. A.tsfile should appear. - Open it and set a breakpoint on a line. If the breakpoint holds and the paused call stack shows your function names, the mapping works.
- In Settings, under Preferences > Sources, make sure Enable JavaScript source maps is checked. It is on by default.
If nothing appears, check these in order:
- The Network panel should show
.js.maprequests with status 200. A 404 means the map file was not copied or the server is blocking it. - The end of the bundle should contain
//# sourceMappingURL=main-XXXX.js.map. If it is missing, you probably havehidden: true. - The map URL must be reachable from where the bundle is served. A CDN or proxy that rewrites paths can break it.
What you will see in the trace
After the first recording, compare it with your expectations.
- Frame names. With maps working, frames from your code show original names and
.tslocations. Without maps you will see short names likea,Qe, ort.ɵfac. - Fewer, shorter frames. Production code is smaller. A deep stack in dev may flatten in production.
- No double check. A component's change detection runs once per cycle, not twice.
- Framework frames. With
vendor: true, names likerefreshViewanddetectChangesInternalshow up, which you will use in later chapters on change detection.
Common mistakes
- Profiling the default dev build and acting on it. Confirm the number in a production-like build before you change code.
- Forgetting that
hidden: truehides the maps. This is a good setting for an error reporting service, but not for DevTools. - Shipping the profiling build. It carries maps and named chunks you probably don't want to expose. Keep it as a separate configuration and don't deploy it.
- Comparing runs from different configurations. If the before trace used
profilingand the after trace usedproduction, the difference may come from the build rather than from your change. Use one configuration for the whole investigation. - Leaving caching on. A stale bundle makes you measure the old code after a rebuild. Check the hash in the bundle name in the Network panel.
Exercise
You built with sourceMap: { scripts: true, hidden: true }, served the output, and opened the Sources panel. No .ts files appear and the Network panel shows no .map requests. What is the cause, and what do you change?
Show answer
With hidden: true the build writes the .map files but omits the //# sourceMappingURL= comment from the bundles, so DevTools never requests them. Set hidden: false (or remove it, since false is the default), rebuild with ng build --configuration profiling, reload with caching disabled, and check that .js.map requests appear with status 200.
Exercise
A teammate records a trace against ng serve and finds that a template method is called twice per change detection cycle. They conclude the method is called twice in production. What is wrong with this conclusion, and how do you check it?
Show answer
In dev mode, Angular runs an extra checking pass after each change detection run to detect expression-changed errors, so expressions are evaluated twice. Production builds skip that pass. Check by recording the same interaction on a production-like build served by a static server. If the method now runs once per cycle, the doubling came from dev mode.