0
0
Angularframework~8 mins

Inline vs external templates in Angular - Performance Comparison

Choose your learning style9 modes available
Performance: Inline vs external templates
MEDIUM IMPACT
This affects the initial page load speed and bundle size by determining how Angular loads and parses component templates.
Choosing how to include Angular component templates
Angular
import { Component } from '@angular/core';
@Component({
  selector: 'app-example',
  templateUrl: './example.component.html'
})
export class ExampleComponent {}
External templates keep the JS bundle smaller and allow the browser to fetch templates separately, enabling parallel loading and faster initial rendering.
📈 Performance GainSaves 5-20kb in JS bundle, reduces blocking time on initial load
Choosing how to include Angular component templates
Angular
import { Component } from '@angular/core';
@Component({
  selector: 'app-example',
  template: `<div><h1>Title</h1><p>Content here</p></div>`
})
export class ExampleComponent {}
Inlining templates increases the JavaScript bundle size, which delays parsing and blocks rendering longer on initial load.
📉 Performance CostAdds 5-20kb to bundle size depending on template length, blocking rendering until JS is parsed
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Inline templateSame DOM nodesSame reflowsSame paint cost[X] Bad (larger JS bundle delays rendering)
External templateSame DOM nodesSame reflowsSame paint cost[OK] Good (smaller JS bundle, faster initial load)
Rendering Pipeline
When templates are inline, the browser must download and parse a larger JS bundle before rendering. External templates allow the browser to fetch HTML separately, enabling parallel downloads and earlier rendering.
Network
Parse & Compile
Render
⚠️ BottleneckParse & Compile stage is most expensive with inline templates due to larger JS bundle size.
Core Web Vital Affected
LCP
This affects the initial page load speed and bundle size by determining how Angular loads and parses component templates.
Optimization Tips
1Use external templates to keep JavaScript bundles smaller and improve initial load speed.
2Inline templates increase bundle size and block rendering longer, hurting LCP.
3External templates enable parallel loading of HTML, reducing blocking time.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance downside of using inline templates in Angular components?
ATriggers more DOM reflows during rendering
BCauses multiple HTTP requests for templates
CIncreases JavaScript bundle size, delaying initial rendering
DPrevents CSS from loading properly
DevTools: Network
How to check: Open DevTools, go to Network tab, reload page, filter by JS and HTML files. Check if templates are loaded as separate HTML files or embedded in JS bundles.
What to look for: Separate HTML template files indicate external templates, which reduce JS bundle size and improve load times.