Click here to Skip to main content
15,880,796 members
Articles / General Programming / Performance
Tip/Trick

Get Angular Watcher Count in any Browser

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
9 Aug 2016CPOL 10.7K   3   5
Get Angular Watcher Count in any Browser, just run this script.

Introduction

AngularJS is indeed a good JQuery programming language, but when it comes to implementing it for enterprise applications and complex pages, it creates lot of performance bottleneck. On AngularJS website also, it has mentioned that watcher count which is anything more than 2000 is a bad Angular code and creates a lot of problems in performance, specially on Internet Explorer browser.

Using the Code

I can't provide you the way to reduce the watcher count, it is a big discussion point and I am sure that every AngularJS developer follows that. Here, I am giving you a simple script which developer has to run into Command/Console window of browser's developer tool window.

JQuery code to be run into Console/Command window is as given below:

JavaScript
document.getElementsByTagName('*').length
for(var root=angular.element(document).injector().get("$rootScope"),
count=root.$$watchers?root.$$watchers.length:0,pendingChildHeads=[root.$$childHead],
currentScope;pendingChildHeads.length;)for(currentScope=pendingChildHeads.shift();
currentScope;)count+=currentScope.$$watchers?currentScope.$$watchers.length:0,
pendingChildHeads.push(currentScope.$$childHead),currentScope=currentScope.$$nextSibling; count;

Points of Interest

Based on my recent experience on large enterprise angular applications, I will work on an article to improve performance. I will link it in this tip once I will do that.

History

  • 1.0

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
Solution Architect with 19 years of experience in IT

Comments and Discussions

 
QuestionGood article Pin
rammanusani26-Mar-19 8:14
rammanusani26-Mar-19 8:14 
QuestionGetting error while executing the code Pin
Bimaln11-Aug-16 20:20
professionalBimaln11-Aug-16 20:20 
AnswerRe: Getting error while executing the code Pin
Joshi, Rushikesh16-Aug-16 11:44
professionalJoshi, Rushikesh16-Aug-16 11:44 
QuestionGetting error while executing on browser console Pin
Sunil Mekala9-Aug-16 19:24
Sunil Mekala9-Aug-16 19:24 
AnswerRe: Getting error while executing on browser console Pin
Joshi, Rushikesh16-Aug-16 11:44
professionalJoshi, Rushikesh16-Aug-16 11:44 
This code will only work if you have Angular application or Angular has been added from Import.

check for this website, which has Angular Code : Angular Google Maps[^]

Thanks
Rushi

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.