Performance: Why components are the building blocks
This concept affects how efficiently the browser renders and updates the UI by managing DOM complexity and reflows.
import { Component } from '@angular/core'; @Component({ selector: 'app-title', template: `<h1>Title</h1>` }) export class TitleComponent {} @Component({ selector: 'app-button', template: `<button (click)="clickHandler()">Click</button>` }) export class ButtonComponent { clickHandler() { console.log('Clicked'); } } @Component({ selector: 'app-root', template: `<app-title></app-title><p>Paragraph</p><app-button></app-button>` }) export class AppComponent {}
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: `<div><h1>Title</h1><p>Paragraph</p><button (click)="clickHandler()">Click</button></div>` }) export class AppComponent { clickHandler() { console.log('Clicked'); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Single large component | High DOM nodes in one tree | Multiple reflows on any change | High paint cost due to large updates | [X] Bad |
| Multiple small components | Smaller DOM per component | Single reflow per component update | Lower paint cost with isolated updates | [OK] Good |
<h1>{{title}}</h1>
<p>Welcome to {{appName}}!</p>export class WelcomeComponent {
title = 'Hello';
appName = 'MyApp';
}@Component({
selector: 'app-test',
template: '<p>Test works!</p>'
})
export class TestComponent {
title = 'Test';
constructor() {
this.title = 123;
}
}Pass the test and this pattern moves unknown