AngularJS is a highly versatile framework and it can be used to build almost any type of web application. Some of the popular web AngularJS applications are — Netflix, LEGO, UpWork, YouTube for PS3, PayPal, Gmail, and The Guardian. Although, AngularJS is capable of handling high volumes of traffic, still, you can skyrocket applications performance by following these measures-
1. Avoid using too much of watchers/data bindings
Any time we introduce more data-bindings, we create more $$watchers and $scopes. It prolongs the digest cycle. Too many $$watchers can cause lag. That’s why you should limit their use as much as possible. One needs to keep a check on the digest cycle. To understand this better, consider each digest cycle as a loop that monitors the changes to variables. The shorter the digest cycle, the faster the application will run.
2. Use native JavaScript or Lodash
Lodash improves your application performance by simply re-writing some of the basic logic instead of relying on inbuilt AngularJS methods. Built-in Angular methods mostly account for generic use cases.
3. Minimize the DOM access
Accessing the DOM very frequently could get expensive, so keep your DOM trees small. Don’t modify the DOM if you can help it, and don’t set any inline styles to avoid JavaScript reflow.
4. Use ng-if instead of ng-show/ng-hide
ng-show directive toggles the CSS display property on a particular element while ng-if directive actually removes the element from DOM and re-creates it (if required). Further, ng-switch directive is an alternative to ng-if for the same AngularJS application performance benefits.
5. Ensure proper Bundling and Minification
Bundling and minifying website scripts and stylesheets reduce page load time and asset size. For Bundling and Minification of code at the time of deployment, you can use several task runners available like gulp or grunt.
[Suggest reading – Working with DOM in Angular: unexpected consequences and optimization techniques]
6. Use $watchCollection instead of $watch
$watch with only 2 parameters is faster. However, Angular also supports a 3rd parameter to this function, that can look like this: $watch(‘value’, function( ){ }, true). The third parameter tells Angular to perform deep checking (i.e. to check every property of the object), which could be very time taking. Thus, for more than 2 parameters, use $watchCollection.
7. Use Chrome DevTools like CPU Profiler and Timeline
A general browser-related technique is to use both the browser devTools Profiler and the Timeline tool. It can help you find performance bottlenecks to guide your optimization efforts.
For further application development related queries, please feel free to write to us at hello@mantralabsglobal.com.
Related blogs-
Knowledge thats worth delivered in your inbox