We all know the speed of our app is very important and in the early stages of development there are rarely any issues with speed, but as the app grows and as we move into testing with real data we can see it getting slow unless we take speed into account throughout development.

When the user interacts with the UI, such as tying in an input box, Angular will run the $digest loop to see if anything has changed. The faster the cycle, the faster the two-way data binding and the better the user experience.

If the $digest loop is very large then the user is going to see lag in the UI and this is definitely something we want to avoid.

One option is to cut down on the number of $digest loops by using ngModelOptions.

ngModelOptions can be declared at the module level so you don’t need to declare it at the individual input field level for every element in your app.

Doing the above would have all the input fields in myApp module inherit the ng-model-options. These can then be overridden in specific input fields if required

You could just use updateOn: ‘default blur’ however the problem with this is that radios and check boxes stop working as expected. By using updateOn: ‘keyup change click blur’ we have found all form elements work as expected (in all browsers we dev for) but it significantly cuts down the number of digest cycles.

As you’re developing you may like to keep track of the number of $digest cycles to ensure your optimising for speed. The below directive will display the count.

You add the below into your UI where you want to see the count.

Another good practice is implementing one-way binding for any data that doesn’t require two-way binding. Just add :: in your templates where required.

 

Tristan Claridge on EmailTristan Claridge on LinkedinTristan Claridge on Twitter
Tristan Claridge
Web Developer at Claridge Media
Tristan is a full stack developer with over 10 years experience developing innovative projects using LAMP, jQuery, AngularJS, NodeJS, and others. He has started and operated a variety of businesses including building the software systems used by, or sold by, the businesses. Tristan grew up in Brisbane, Australia, and has a couple of degrees from the University of Queensland. Tristan now lives in Dunedin, New Zealand, enjoying world-class internet speeds and the beautiful country.