Performance: Standalone pipes and directives
This concept affects the initial page load speed and bundle size by allowing Angular to load only the necessary pipes and directives without extra module overhead.
Jump into concepts and practice - no test required
import { Component } from '@angular/core'; import { MyPipe } from './my-pipe'; @Component({ selector: 'app-example', template: '{{ value | myPipe }}', standalone: true, imports: [MyPipe] }) export class ExampleComponent {}
import { Component, NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; import { MyPipe } from './my-pipe'; @NgModule({ declarations: [MyPipe], exports: [MyPipe], imports: [CommonModule] }) export class SharedModule {} @Component({ selector: 'app-example', template: '{{ value | myPipe }}', standalone: true, imports: [SharedModule] }) export class ExampleComponent {}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| NgModule imports for pipes/directives | More nodes and directives processed | Multiple reflows due to style recalculations | Higher paint cost from complex styles | [X] Bad |
| Standalone pipes/directives imports | Minimal nodes processed | Single reflow | Lower paint cost | [OK] Good |
standalone: true in an Angular pipe or directive decorator do?standalone: true flag in Angular marks a pipe or directive so it does not require declaration inside an NgModule.'[appHighlight]'.standalone: true must be set in the decorator.@Pipe({name: 'exclaim', standalone: true})
export class ExclaimPipe implements PipeTransform {
transform(value: string): string {
return value + '!';
}
}<div>{{ 'Hello' | exclaim }}</div>@Directive({ selector: '[appColor]', standalone: true })
export class ColorDirective {
constructor(private el: ElementRef) {
el.nativeElement.style.color = 'red';
}
}<div appColor>Text</div> in a component template but forget to import ColorDirective in the component, what happens?standalone: true in the pipe decorator.imports array.standalone: true allows the pipe to be used without NgModule declaration.imports array and then use it in templates.