perf(forms): optimize internal method _anyControls in FormGroup (#32534)

The method was previously looping through all controls, even after finding at least one that
satisfies the provided condition. This can be a bottleneck with large forms. The new version
of the method returns as soon as a single control which conforms to the condition is found.

PR Close #32534
This commit is contained in:
lazarljubenovic 2019-09-07 17:06:46 +02:00 committed by Misko Hevery
parent e0dfa42d6e
commit 2a145f2463
1 changed files with 7 additions and 5 deletions

View File

@ -1593,11 +1593,13 @@ export class FormGroup extends AbstractControl {
/** @internal */
_anyControls(condition: Function): boolean {
let res = false;
this._forEachChild((control: AbstractControl, name: string) => {
res = res || (this.contains(name) && condition(control));
});
return res;
for (const controlName of Object.keys(this.controls)) {
const control = this.controls[controlName];
if (this.contains(controlName) && condition(control)) {
return true;
}
}
return false;
}
/** @internal */