Performance Profiling
All About Profiling
Section titled “All About Profiling”What is Profiling?
By definition Profiling is a form of dynamic program analysis that measures, for example, the space (memory) or time complexity of a program, the usage of particular instructions, or the frequency and duration of function calls.
Why is it necessary?
Profiling is important because you can’t optimise effectively until you know what your program is spending most of its time doing. Without measuring your program execution time (profiling), you won’t know if you’ve actually improved it.
Tools and Techniques :
- Use the following code to manually find out the number of watchers in your angular app (credit to @Words Like Jared Number of watchers)
(function() { var root = angular.element(document.getElementsByTagName('body')), watchers = [], f = function(element) { angular.forEach(['$scope', '$isolateScope'], function(scopeProperty) { if(element.data() && element.data().hasOwnProperty(scopeProperty)) { angular.forEach(element.data()[scopeProperty].$$watchers, function(watcher) { watchers.push(watcher); }); } });
angular.forEach(element.children(), function(childElement) { f(angular.element(childElement)); }); };
f(root);
// Remove duplicate watchers var watchersWithoutDuplicates = []; angular.forEach(watchers, function(item) { if(watchersWithoutDuplicates.indexOf(item) < 0) { watchersWithoutDuplicates.push(item); } }); console.log(watchersWithoutDuplicates.length);})();Next Steps:
Done with Profiling. It only brings you half way down the road. The very next task is to actually turn your findings into action items to optimise your application. See this documentation on how you can improve the performance of your angular app with simple tricks.
Happy Coding :)