SlideShare a Scribd company logo
Performance
Angularjs
What is Angulars biggest
performance bottleneck?
The $digest cycle (a.k.a. dirty
checking)
How the $digest cycle works
When Angular (or you) calls $scope.$apply, this kicks off the $digest cycle.
$rootScope
$scope
$scope $scope.$apply
How the $digest cycle works
During the digest cycle, Angular will check every $scope for all Angular
bindings to see if any have changed.
$rootScope
$scope
ngBinding
ngBinding
$scope
ngBinding
Changed?
How the $digest cycle works
If any binding has changed, Angular will finish the current cycle and then start
the loop over again checking to see if the change affected other bindings.
$rootScope
$scope
ngBinding
ngBinding
$scope
ngBinding
Changed? Yes
Changed? No
Changed? No (restart cycle)
How the $digest cycle works
A $digest cycle will only complete when no Angular bindings have changed.
$rootScope
$scope
ngBinding
ngBinding
$scope
ngBinding
Changed? No
Changed? No
Changed? No ($digest completed)
How the $digest cycle works
This means that any change will cause the $digest cycle to run at least twice;
once to update the binding and again to ensure that no other bindings were
changed.
Example: If your app has 200 bindings, Angular will run at least 400 dirty
checks when a binding changes.
Improving $digest performance
● Reduce number of bindings
o use native DOM
o ng-if instead of ng-hide
o clean up $watchers when no longer needed
o avoid ng-repeat where possible
o avoid filters where possible
● Simplify logic in bindings
o use simple comparisons
o avoid binding to functions where possible
Use Native DOM
Just because you are using Angular as a framework does not mean you have
to do everything the “Angular” way.
One of the nice things about Angular is that you can choose when and when
not to use it.
If your DOM won’t change very often or only at certain, controllable times, you
can use native DOM manipulation instead of relying on Angular bindings.
Code Example
Use ng-if instead of ng-show
ng-show just hides the DOM using display:none; However, this keeps all the
bindings of the element and it’s children.
ng-if removes the DOM and all of it’s bindings.
caveat: ng-if manipulates the DOM, which can become more expensive if the
ng-if changes state a lot.
Clean up $watchers
Sometimes you only need to $watch for a change once and then no longer
need to watch for any changes anymore.
The $scope.$watch() function returns a function that you can use to remove the
watch binding.
var unWatch = $scope.$watch(‘contact’, function() {
// do some logic
unWatch(); // unbind watcher as no longer needed
});
Avoid ng-repeat
ng-repeat has some big performance problems, especially with large data sets.
ng-repeat adds lots of bindings to the DOM (1 for itself + n bindings inside the
repeat * num of items in collection)
<div ng-repeat=”item in list”>
<div>{{hello}} <span>{{goodbye}}</span></div>
</div>
bindings = (2 bindings * list length) + ng-repeat [list of 10 items = 21 bindings]
Avoid ng-repeat
Each of these bindings will be checked on every $digest, even if the list didn’t
change.
Even worse, when the list does change, it will manipulate the DOM, which
depending on what you are doing, can be expensive.
This is the exact same problem with filters.
Avoid filters
Filters will be run on every $digest, even if the binding attached to the filter
didn’t change.
Filters such as lowercase, date, and orderBy are bad because they will
transform the DOM at every $digest.
Instead, you can use the $filter provider and save the filtered result to be
displayed into the DOM.
Code Example
Use Simple Logic in Bindings
Because dirty-checking requires a comparison check of the old value to the
new value, the faster the comparison the faster the $digest cycle will run.
Comparisons to primitives and shallow equality of objects are the fastest.
Deep equality of objects are slow (so don’t do it).
Avoid Functions in Bindings
Functions in bindings are bad from a performance point of view.
A function inside a binding ( {{fn()}} ) will be called at every $digest (having
function call overhead).
A function inside an ng-repeat will be called for every item in the collection.
This doesn’t mean that functions inside ng-click are bad, since it will only be
called when the element is clicked on.
Avoid Functions in Bindings
Types of Functions:
● Just returns variable - get rid of the function and just use the variable
directly
● Performs some logic - try to pre-compute the logic once and then save the
result to be used in a binding

More Related Content

What's hot (20)

PDF
Advanced Tips & Tricks for using Angular JS
Simon Guest
 
PDF
AngularJS Basics with Example
Sergey Bolshchikov
 
PPTX
AngularJs
syam kumar kk
 
PDF
Workshop 14: AngularJS Parte III
Visual Engineering
 
ODP
AngularJs Crash Course
Keith Bloomfield
 
PDF
Integrating Angular js & three.js
Josh Staples
 
PDF
Patterns Are Good For Managers
AgileThought
 
PDF
Zenly - Reverse geocoding
CocoaHeads France
 
PDF
Workshop 12: AngularJS Parte I
Visual Engineering
 
PPTX
AngularJS with TypeScript and Windows Azure Mobile Services
Rainer Stropek
 
PPTX
Angularjs Basics
Anuradha Bandara
 
PDF
AngularJS Framework
Barcamp Saigon
 
PPTX
AngularJS Directives
Eyal Vardi
 
PPTX
AngularJS Animations
Eyal Vardi
 
PDF
AngularJS best-practices
Henry Tao
 
PDF
AngularJS Deep Dives (NYC GDG Apr 2013)
Nitya Narasimhan
 
PDF
React.js触ってみた 吉澤和香奈
Wakana Yoshizawa
 
PDF
Intro to Angular.JS Directives
Christian Lilley
 
PDF
AngularJS performance & production tips
Nir Kaufman
 
PDF
The Art of AngularJS - DeRailed 2014
Matt Raible
 
Advanced Tips & Tricks for using Angular JS
Simon Guest
 
AngularJS Basics with Example
Sergey Bolshchikov
 
AngularJs
syam kumar kk
 
Workshop 14: AngularJS Parte III
Visual Engineering
 
AngularJs Crash Course
Keith Bloomfield
 
Integrating Angular js & three.js
Josh Staples
 
Patterns Are Good For Managers
AgileThought
 
Zenly - Reverse geocoding
CocoaHeads France
 
Workshop 12: AngularJS Parte I
Visual Engineering
 
AngularJS with TypeScript and Windows Azure Mobile Services
Rainer Stropek
 
Angularjs Basics
Anuradha Bandara
 
AngularJS Framework
Barcamp Saigon
 
AngularJS Directives
Eyal Vardi
 
AngularJS Animations
Eyal Vardi
 
AngularJS best-practices
Henry Tao
 
AngularJS Deep Dives (NYC GDG Apr 2013)
Nitya Narasimhan
 
React.js触ってみた 吉澤和香奈
Wakana Yoshizawa
 
Intro to Angular.JS Directives
Christian Lilley
 
AngularJS performance & production tips
Nir Kaufman
 
The Art of AngularJS - DeRailed 2014
Matt Raible
 

Similar to Angularjs Performance (20)

PPTX
Dive into Angular, part 3: Performance
Oleksii Prohonnyi
 
PDF
Javascript Memory leaks and Performance & Angular
Erik Guzman
 
PPTX
Optimizing a large angular application (ng conf)
A K M Zahiduzzaman
 
PPT
Angular js meetup
Pierre-Yves Gicquel
 
PPT
angularjsmeetup-150303044616-conversion-gate01
Teo E
 
PPTX
Angular Data Binding
Duy Khanh
 
PDF
Dragos Rusu - Angular JS - Overcoming Performance Issues - CodeCamp-10-may-2014
Codecamp Romania
 
PPTX
Optimizing Angular Performance in Enterprise Single Page Apps
Morgan Stone
 
PPTX
AngularJS Best Practices
Narek Mamikonyan
 
PPTX
AngularJs presentation
Phan Tuan
 
PPT
Angular Seminar-js
Mindfire Solutions
 
PDF
5 benefits of angular js
OnGraph Technologies Pvt. Ltd.
 
PDF
AngularJS in practice
Eugene Fidelin
 
PPTX
Bhuvi ppt zerobug
BhuviS3
 
PPTX
5 angularjs features
Alexey (Mr_Mig) Migutsky
 
PPTX
Angular workshop - Full Development Guide
Nitin Giri
 
PDF
AngularJS Basics
Ravi Mone
 
PDF
AngularJS Workshop
Gianluca Cacace
 
PPTX
Scope demystified - AngularJS
Sumanth krishna
 
PPTX
Angular Presentation
Adam Moore
 
Dive into Angular, part 3: Performance
Oleksii Prohonnyi
 
Javascript Memory leaks and Performance & Angular
Erik Guzman
 
Optimizing a large angular application (ng conf)
A K M Zahiduzzaman
 
Angular js meetup
Pierre-Yves Gicquel
 
angularjsmeetup-150303044616-conversion-gate01
Teo E
 
Angular Data Binding
Duy Khanh
 
Dragos Rusu - Angular JS - Overcoming Performance Issues - CodeCamp-10-may-2014
Codecamp Romania
 
Optimizing Angular Performance in Enterprise Single Page Apps
Morgan Stone
 
AngularJS Best Practices
Narek Mamikonyan
 
AngularJs presentation
Phan Tuan
 
Angular Seminar-js
Mindfire Solutions
 
5 benefits of angular js
OnGraph Technologies Pvt. Ltd.
 
AngularJS in practice
Eugene Fidelin
 
Bhuvi ppt zerobug
BhuviS3
 
5 angularjs features
Alexey (Mr_Mig) Migutsky
 
Angular workshop - Full Development Guide
Nitin Giri
 
AngularJS Basics
Ravi Mone
 
AngularJS Workshop
Gianluca Cacace
 
Scope demystified - AngularJS
Sumanth krishna
 
Angular Presentation
Adam Moore
 
Ad

Recently uploaded (20)

PDF
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
PDF
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
PDF
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
PPTX
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
PPT
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
PPTX
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
PDF
Executive Business Intelligence Dashboards
vandeslie24
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PPTX
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
PDF
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
DOCX
Import Data Form Excel to Tally Services
Tally xperts
 
Digger Solo: Semantic search and maps for your local files
seanpedersen96
 
Engineering the Java Web Application (MVC)
abhishekoza1981
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Unlock Efficiency with Insurance Policy Administration Systems
Insurance Tech Services
 
Odoo CRM vs Zoho CRM: Honest Comparison 2025
Odiware Technologies Private Limited
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pdf
Varsha Nayak
 
Revolutionizing Code Modernization with AI
KrzysztofKkol1
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
MergeSortfbsjbjsfk sdfik k
RafishaikIT02044
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
Platform for Enterprise Solution - Java EE5
abhishekoza1981
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
Executive Business Intelligence Dashboards
vandeslie24
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Tally_Basic_Operations_Presentation.pptx
AditiBansal54083
 
Alexander Marshalov - How to use AI Assistants with your Monitoring system Q2...
VictoriaMetrics
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Import Data Form Excel to Tally Services
Tally xperts
 
Ad

Angularjs Performance

  • 2. What is Angulars biggest performance bottleneck?
  • 3. The $digest cycle (a.k.a. dirty checking)
  • 4. How the $digest cycle works When Angular (or you) calls $scope.$apply, this kicks off the $digest cycle. $rootScope $scope $scope $scope.$apply
  • 5. How the $digest cycle works During the digest cycle, Angular will check every $scope for all Angular bindings to see if any have changed. $rootScope $scope ngBinding ngBinding $scope ngBinding Changed?
  • 6. How the $digest cycle works If any binding has changed, Angular will finish the current cycle and then start the loop over again checking to see if the change affected other bindings. $rootScope $scope ngBinding ngBinding $scope ngBinding Changed? Yes Changed? No Changed? No (restart cycle)
  • 7. How the $digest cycle works A $digest cycle will only complete when no Angular bindings have changed. $rootScope $scope ngBinding ngBinding $scope ngBinding Changed? No Changed? No Changed? No ($digest completed)
  • 8. How the $digest cycle works This means that any change will cause the $digest cycle to run at least twice; once to update the binding and again to ensure that no other bindings were changed. Example: If your app has 200 bindings, Angular will run at least 400 dirty checks when a binding changes.
  • 9. Improving $digest performance ● Reduce number of bindings o use native DOM o ng-if instead of ng-hide o clean up $watchers when no longer needed o avoid ng-repeat where possible o avoid filters where possible ● Simplify logic in bindings o use simple comparisons o avoid binding to functions where possible
  • 10. Use Native DOM Just because you are using Angular as a framework does not mean you have to do everything the “Angular” way. One of the nice things about Angular is that you can choose when and when not to use it. If your DOM won’t change very often or only at certain, controllable times, you can use native DOM manipulation instead of relying on Angular bindings. Code Example
  • 11. Use ng-if instead of ng-show ng-show just hides the DOM using display:none; However, this keeps all the bindings of the element and it’s children. ng-if removes the DOM and all of it’s bindings. caveat: ng-if manipulates the DOM, which can become more expensive if the ng-if changes state a lot.
  • 12. Clean up $watchers Sometimes you only need to $watch for a change once and then no longer need to watch for any changes anymore. The $scope.$watch() function returns a function that you can use to remove the watch binding. var unWatch = $scope.$watch(‘contact’, function() { // do some logic unWatch(); // unbind watcher as no longer needed });
  • 13. Avoid ng-repeat ng-repeat has some big performance problems, especially with large data sets. ng-repeat adds lots of bindings to the DOM (1 for itself + n bindings inside the repeat * num of items in collection) <div ng-repeat=”item in list”> <div>{{hello}} <span>{{goodbye}}</span></div> </div> bindings = (2 bindings * list length) + ng-repeat [list of 10 items = 21 bindings]
  • 14. Avoid ng-repeat Each of these bindings will be checked on every $digest, even if the list didn’t change. Even worse, when the list does change, it will manipulate the DOM, which depending on what you are doing, can be expensive. This is the exact same problem with filters.
  • 15. Avoid filters Filters will be run on every $digest, even if the binding attached to the filter didn’t change. Filters such as lowercase, date, and orderBy are bad because they will transform the DOM at every $digest. Instead, you can use the $filter provider and save the filtered result to be displayed into the DOM. Code Example
  • 16. Use Simple Logic in Bindings Because dirty-checking requires a comparison check of the old value to the new value, the faster the comparison the faster the $digest cycle will run. Comparisons to primitives and shallow equality of objects are the fastest. Deep equality of objects are slow (so don’t do it).
  • 17. Avoid Functions in Bindings Functions in bindings are bad from a performance point of view. A function inside a binding ( {{fn()}} ) will be called at every $digest (having function call overhead). A function inside an ng-repeat will be called for every item in the collection. This doesn’t mean that functions inside ng-click are bad, since it will only be called when the element is clicked on.
  • 18. Avoid Functions in Bindings Types of Functions: ● Just returns variable - get rid of the function and just use the variable directly ● Performs some logic - try to pre-compute the logic once and then save the result to be used in a binding