From 2a145f24636a1431ec7fb7b6f3def3b99383ba95 Mon Sep 17 00:00:00 2001 From: lazarljubenovic Date: Sat, 7 Sep 2019 17:06:46 +0200 Subject: [PATCH] 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 --- packages/forms/src/model.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/forms/src/model.ts b/packages/forms/src/model.ts index 51dcb5937a..7aefdd8b01 100644 --- a/packages/forms/src/model.ts +++ b/packages/forms/src/model.ts @@ -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 */