Trung BìnhAngular iconAngular

Pure pipe và impure pipe khác nhau thế nào?

Pipe transform dữ liệu trong template. Mặc định pipe là pure: Angular chỉ gọi lại khi primitive value đổi hoặc object/array reference đổi, nên hiệu năng tốt hơn.

Ví dụ custom pure pipe:

typescript
@Pipe({ name: "kebabCase" })
export class KebabCasePipe implements PipeTransform {
  transform(value: string): string {
    return value.toLowerCase().replaceAll(" ", "-")
  }
}

Impure pipe pure: false có thể bắt mutation bên trong object/array nhưng chạy rất thường xuyên, dễ gây chậm UI.

Trong phỏng vấn nên nói: ưu tiên pure pipe, immutable data, hoặc computed() thay vì impure pipe.

Xem toàn bộ Angular cùng filter theo level & chủ đề con.

Mở danh sách Angular