Jobsdev
Jobsdev

Reputation: 1039

Performance - Should two way data binding be avoided?

Sorry if the question is too basic.

I want to know if I should avoid two way data binding in my system. I can capture the values of the form in the submit the form without having to use two way data binding.

My question is whether the two way data binding actually consumes too much memory. Should I stop using it?

Upvotes: 5

Views: 6122

Answers (2)

Steve Land
Steve Land

Reputation: 4862

Short answer: Yes, use it (in my opinion).

Performance wise, there was a problem with the way AngularJS (version 1) handled two way data binding, but this has been largely resolved in Angular 2+ (to the extent that its not something you need to concern yourself about in the vast majority of situations if at all).

It is however worth reading about how Angular handles 2 way binding because this will improve your understanding of the syntax and how you can use it to suit the context - Throughtram has a good blog on it here: https://blog.thoughtram.io/angular/2016/10/13/two-way-data-binding-in-angular-2.html

Whether it is "good practice" to use it, this is certainly debatable. While I agree that Denis Reshetniak's answer makes a valid point, I personally use it frequently and I would suggest that the time saved and greater simplicity of code more than makes up for any drawbacks and occasional issues you may run into (I've not had a problem yet and have been using it substantially since the version 2 release). At the end of the day, it depends on your situation and requirements - you should ensure you understand the pros and cons and then decide which approach suits your use case best.

Upvotes: 5

Denys Reshetniak
Denys Reshetniak

Reputation: 382

It's not about memory or performance. It's more about unidirectional data flow convention. You can split [(ngModel)] two way for input [ngModel]=data and output (ngModelChange)="callback($event)". And you will be sure that data can be change only in one direction. Second advantage of this approach is in custom behavior when you need to change the data from input.

Upvotes: 2

Related Questions