Performance: FormArray for dynamic fields
MEDIUM IMPACT
This affects the rendering speed and responsiveness when managing multiple dynamic form controls in Angular.
this.form = new FormGroup({ fields: new FormArray([]) }); addField() { (this.form.get('fields') as FormArray).push(new FormControl('')); } removeField(index: number) { (this.form.get('fields') as FormArray).removeAt(index); }
this.form = new FormGroup({ fields: new FormGroup({ field1: new FormControl(''), field2: new FormControl(''), // ...manually adding each field }) }); // Adding/removing fields requires recreating the whole FormGroup
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual FormGroup for each dynamic field | High (many nodes added/removed individually) | Multiple reflows per field change | High paint cost due to full form re-render | [X] Bad |
| Using FormArray for dynamic fields | Low (only affected controls updated) | Single reflow per add/remove | Low paint cost with targeted updates | [OK] Good |