Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Angular 2 have better performance than Angular 1?

Wonderful developers, I need a little help clarifying why Angular 2 is more performant than Angular 1?

After searching the web, I came up with this relevant explanation from Er Shahbaz Sharif in a Quora discussion:

Angular 2 is using Hierarchical Dependency Injection system which is major performance booster. 
Angular 2 implements unidirectional tree based change detection which again increases performance . 
As per ng-conf meetup, angular 2 is 5 times faster as compared to angular 1

Other explanations in the same discussion seem to hover around this as well.

Could somebody please clarify for me why those 2 factors (and others, if any) help boost the performance of Angular 2? Thanks so much..

like image 319
JustAPup Avatar asked Aug 05 '16 02:08

JustAPup


2 Answers

Angular2 - doesn't do deep object compairson. If items are added/removed to/from an array, change detection won't detect it. The same for object properties as long as they are not directly bound from the view.

  • Two-way binding is split into propagating detected changes from root to leaves only.

  • Changes from child to parent are only propagated by explicit events (outputs), only to direct parents.

  • ChangeDetectionStrategy.OnPush doesn't run change detection for components when no input values have changes. This prunes subtrees of components where change detection isn't run at all.

  • The code (especially the change detection code) is written in a way that it can be optimized as much as possible by the JS VMs.

https://www.quora.com/What-is-the-difference-between-angularjs-and-angular2

like image 60
Günter Zöchbauer Avatar answered Oct 14 '22 12:10

Günter Zöchbauer


One of the reasons for performance is the possibility of the existence of change detection loops.

  • Angular1 hamstrung its performance with how it polled state. During every digest cycle, the framework checks if any of hundreds or thousands of values in your app changed.
  • Angular 2 will allow the developer to provide some guarantees to the change detection mechanism to avoid scanning parts of the component tree. For more details, you can find more at here.
like image 22
Ha Hoang Avatar answered Oct 14 '22 11:10

Ha Hoang