r/angularjs • u/gregory_k • Sep 24 '14
[Resource] Optimizing AngularJS from 1200ms to 35ms
http://blog.scalyr.com/2013/10/angularjs-1200ms-to-35ms/2
u/brianvaughn Sep 24 '14
Interesting optimizations. Particularly the digest-intercept approach :) thanks for sharing!
2
u/lemon-meringue Sep 25 '14
What's the difference between using optimization #4 and using ng-if? From my experience, correct me if I'm wrong, because ng-if removes the elements, it also removes the watches.
I suppose there may be a slight performance penalty compared to optimization #4 as you have to recreate the DOM nodes, but this should be minor?
1
1
Sep 25 '14
I've used scalyr a while ago and dug a bit in its code. If I'm not mistaking #4 is so the elements remain in the page. With ng-if, they're added/removed and they're also re-compiled when they're re-added. Scalyr is trying to squeeze every bit of caching it can from the browser; it's not made for everyday use, only for those cases when you have a lot of data to render, so at first glance something like this may seem useless but it really comes in handy when you have dozens of thousands of elements to display.
2
u/gatman02 Sep 25 '14
This is great! Thanks for sharing it to the community. I'm starting to run into these problems myself (as are many other angular developers).
2
u/WarInternal Sep 25 '14
As someone who is new to AngularJS, could somebody explain why each word had a change watcher? Is this a side-effect of ng-repeat? Or am I confusing it with something else, maybe?
1
u/doctorjokie Sep 25 '14
Their business case was to make every word in the log line linkable. Therefore, there was a watcher associated with each word.
2
u/WarInternal Sep 25 '14
I apologize, I worded that incorrectly. I'm more interested in the mechanics behind it - What part of it was establishing the watcher. For instance, if they had slapped an ng-onclick='function(this)' on each word, would that have that established a 'change watcher'?
2
u/gatman02 Sep 25 '14
Every property added to a scope is watched automatically. Alternatively, you can explicitly add things to watch using $watch.
In the application mentioned here, each word had to be wrapped in an html tag in order to make that word clickable. My guess is that in order to do this originally, they converted their text log file into an array of words, then used ng-repeat to display each word with the specified html wrapper. ng-repeat adds a watchers for each element in its array.
1
1
u/heat_forever Sep 29 '14
Not sure why they don't add a parameter to ng-repeat to customize the level of watchers created. I'm guessing the current implementation handles all cases (very inefficiently) but I think a developer should be able to say:
Watch only the collection reference (majority probably replace the whole collection from an AJAX call anyway).
Watch every object/row in the collection (to handle add/remove/delete of an entire row scenario).
Default to current behavior (handle any possible change in the entire collection which means adding a watcher for every element in every object in the collection).
2
u/gatman02 Sep 25 '14
Also, to answer your other question, using ng-click does not establish a watcher, but rather forces a $digest loop, which checks all the watchers for changes.
1
u/christophermoll Sep 25 '14
I keep the number of watchers down by using bindonce extensively. It's just wasteful to bind all the data with ng-bind or {{ }}. When used properly it seems like it achieves the same results as #2 and #4. The others are interesting, though.
2
2
u/Asmor Sep 25 '14
Wow, very neat! Any chance of releasing the code?