Performance: Why directives are needed
MEDIUM IMPACT
Directives affect how efficiently Angular manipulates the DOM and updates the UI, impacting rendering speed and responsiveness.
@Directive({ selector: '[appHighlight]' }) export class HighlightDirective { constructor(private el: ElementRef) { el.nativeElement.style.backgroundColor = 'yellow'; } }Manipulating DOM directly inside component code using document.querySelector and manual event listeners.| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct DOM manipulation in components | High (manual queries and listeners) | Multiple reflows per update | High paint cost due to layout thrashing | [X] Bad |
| Using Angular directives | Low (Angular manages DOM efficiently) | Single reflow per directive | Low paint cost with optimized updates | [OK] Good |