dive into angular, part 3: performance

31
DIVE INTO ANGULAR, PART 3: PERFORMANCE _by Oleksii Prohonnyi

Upload: oleksii-prohonnyi

Post on 16-Apr-2017

178 views

Category:

Software


6 download

TRANSCRIPT

Page 1: Dive into Angular, part 3: Performance

DIVE INTO ANGULAR,PART 3: PERFORMANCE

_by Oleksii Prohonnyi

Page 2: Dive into Angular, part 3: Performance

AGENDA How to measure performance Watchers minimization Bind once Filters and cache Ng-Repeat Ng-Model Ng-If vs Ng-Show $$apply vs $$digest References

Page 3: Dive into Angular, part 3: Performance

HOW TO MEASURE PERFORMANCE

Page 5: Dive into Angular, part 3: Performance

HOW TO MEASURE PERFORMANCE: DEV.TOOLS

Page 6: Dive into Angular, part 3: Performance

HOW TO MEASURE PERFORMANCE: BATARANG

Page 7: Dive into Angular, part 3: Performance

HOW TO MEASURE PERFORMANCE: CONSOLE

Page 8: Dive into Angular, part 3: Performance

WATCHERS MINIMIZATION

Page 9: Dive into Angular, part 3: Performance

WATCHERS MINIMIZATION Angular uses dirty checking to keep track of all the changes in

app. This means it will have to go through every watcher to check if

they need to be updated (call the digest cycle). If one of the watcher is relied upon by another watcher, Angular

would have to re-run the digest cycle again, to make sure that all of the changes has propagated.

It will continue to do so, until all of the watchers have been updated and app has stabilized.

Page 10: Dive into Angular, part 3: Performance

WATCHERS MINIMIZATIONWatches are set on: $scope.$watch {{ }} type bindings Most directives (i.e. ng-show) Scope variables scope: { bar: '='} Filters {{ value | myFilter }} ng-repeat

Page 11: Dive into Angular, part 3: Performance

WATCHERS MINIMIZATIONWatchers (digest cycle) run on: User action (ng-click etc). Most built in directives will call

$scope.apply upon completion which triggers the digest cycle.

ng-change ng-model $http events (so all ajax calls) $q promises resolved $timeout $interval Manual call to $scope.apply and $scope.digest

Page 12: Dive into Angular, part 3: Performance

BIND ONCE

Page 13: Dive into Angular, part 3: Performance

BIND ONCE Angular 1.3 added :: notation to allow one time binding. In summary, Angular will wait for a value to stabilize after it’s first

series of digest cycles, and will use that value to render the DOM element.

After that, Angular will remove the watcher forgetting about that binding.

See more: code.angularjs.org

Page 14: Dive into Angular, part 3: Performance

FILTERS AND CACHE

Page 15: Dive into Angular, part 3: Performance

FILTERS AND CACHE One time binding does not seem to play well with filters. There seems to be workarounds to make it work, but I think it’s

cleaner and more intuitive to simply assign the needed value to a variable (or set it as a property on an object, if you are dealing with a lot of variables).

See more: code.angularjs.org

Page 16: Dive into Angular, part 3: Performance

FILTERS AND CACHE Instead (HTML):

Do (JavaScript):

Do (HTML):

Page 17: Dive into Angular, part 3: Performance

FILTERS AND CACHE Instead (HTML):

Do (JavaScript):

Do (HTML):

Page 18: Dive into Angular, part 3: Performance

NG-REPEAT

Page 19: Dive into Angular, part 3: Performance

NG-REPEATngRepeat uses $watchCollection to detect changes in the collection. When a change happens, ngRepeat then makes the corresponding changes to the DOM: When an item is added, a new instance of the template is added

to the DOM. When an item is removed, its template instance is removed from

the DOM. When items are reordered, their respective templates are

reordered in the DOM.

See more: code.angularjs.org

Page 20: Dive into Angular, part 3: Performance

NG-MODEL

Page 21: Dive into Angular, part 3: Performance

NG-MODEL If you know there is going to be a lot of changes coming from an

ng-model, you can de-bounce the input. For example if you have a search input like Google, you can de-

bounce it by setting the following ng-model option:ng-model-options="{ debounce: 250 }.

This will ensure that the digest cycle due to the changes in this input model will get triggered no more then once per 250ms .

See more: code.angularjs.org

Page 22: Dive into Angular, part 3: Performance

NG-IF VS NG-SHOW

Page 23: Dive into Angular, part 3: Performance

NG-IF VS NG-SHOW ng-show will render an element, and use display:none to

hide it, ng-if will actually removes the element from DOM, and will re-

create it, if it’s needed. You may need ng-show for an elements that toggles on an off

often, but for 95% of the time, ng-if is a better way to go.

See more: docs.angularjs.org, docs.angularjs.org

Page 24: Dive into Angular, part 3: Performance

$$APPLY VS $$DIGEST

Page 25: Dive into Angular, part 3: Performance

$$APPLY VS $$DIGEST When $scope.$apply() is called, it kicks the entire

application into the $digest loop and in turn runs $rootScope.$digest().

This is what actually kicks off the internal $digest cycle. This cycle processes all of the watchers of the $scope it was

called from (and its children) until no more listeners can be fired. Instead of $scope.$apply, we could turn to $scope.

$digest, which runs the exact same $digest loop, but is executed from the current $scope downwards through its children, a much less costly venture.

See more: binpress.com

Page 26: Dive into Angular, part 3: Performance

REFERENCES

Page 27: Dive into Angular, part 3: Performance

REFERENCES https://ng-perf.com/ http://www.codelord.net/2014/06/17/angular-performance-101-sli

des/

http://www.alexkras.com/11-tips-to-improve-angularjs-performance/

https://medium.com/developers-writing/tips-and-practices-to-optimize-your-angularjs-app-8118550ff808#.4vmopptk9

http://blog.scalyr.com/2013/10/angularjs-1200ms-to-35ms/ https://

www.binpress.com/tutorial/speeding-up-angular-js-with-simple-optimizations/135

https://www.airpair.com/angularjs/posts/angularjs-performance-large-applications

Page 28: Dive into Angular, part 3: Performance
Page 29: Dive into Angular, part 3: Performance

HOME TASK

Page 30: Dive into Angular, part 3: Performance

HOME TASK Main idea: ruin performance of the application created after 2nd

meetup and optimize it using recommendations described above. Technical details:

– Update your application with more data, directives, DOM to gather bad performance issues in it:

• at least 1000 rows in table on home page,• at least 2000 watchers in the application.

– Optimize it using recommendations from today’s meeting. Download 2nd meetup’s application: github.com

Page 31: Dive into Angular, part 3: Performance

Oleksii Prohonnyi

facebook.com/oprohonnyi

linkedin.com/in/oprohonnyi