Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Angular: Tuning The Engine

Angular: Tuning The Engine

Dive into the Angular event loop, how the $digest cycle works and performance tuning tips and tricks.

Todd Motto

March 15, 2016
Tweet

More Decks by Todd Motto

Other Decks in Programming

Transcript

  1. < i n p u t n g - m

    o d e l = " t i t l e " > < d i v > I n p u t v a l u e : < s p a n > { { t i t l e } } < / s p a n > < / d i v >
  2. S c o p e { $ $ w a

    t c h e r s : [ { e q : f a l s e , e x p : f u n c t i o n ( ) { } , f n : f u n c t i o n ( n e w V a l u e , o l d V a l u e ) { } , g e t : f u n c t i o n ( ) { } , l a s t : ' T o d d ' } ] }
  3. f u n c t i o n M a

    i n C o n t r o l l e r ( $ s c o p e ) { $ s c o p e . n a m e = ' T o d d ' ; $ s c o p e . $ w a t c h ( ' n a m e ' , f u n c t i o n ( n e w V a l u e , o l d V a l u e ) { / / d o s o m e t h i n g w i t h n e w V a l u e } ) ; } a n g u l a r . m o d u l e ( ' a p p ' ) . c o n t r o l l e r ( ' M a i n C o n t r o l l e r ' , M a i n C o n t r o l l e r ) ;
  4. S c o p e { $ $ w a

    t c h e r s : [ { e q : f a l s e , e x p : f u n c t i o n ( ) { } , f n : f u n c t i o n ( n e w V a l u e , o l d V a l u e ) { } , g e t : f u n c t i o n ( ) { } , l a s t : ' T o d d ' } ] }
  5. f u n c t i o n M a

    i n C o n t r o l l e r ( $ s c o p e ) { $ s c o p e . n a m e = ' T o d d ' ; } a n g u l a r . m o d u l e ( ' a p p ' ) . c o n t r o l l e r ( ' M a i n C o n t r o l l e r ' , M a i n C o n t r o l l e r ) ;
  6. < ! - - T H I S c r

    e a t e s t h e " w a t c h e r " - - > < p > { { n a m e } } < / p >
  7. S c o p e { $ $ w a

    t c h e r s : [ { e q : f a l s e , e x p : f u n c t i o n ( ) { } , f n : f u n c t i o n ( n e w V a l u e , o l d V a l u e ) { } , g e t : f u n c t i o n ( ) { } , l a s t : ' T o d d ' } ] }
  8. Angular uses $$watchers to update the DOM Angular uses $$watchers

    to sync our Model $$watchers impact performance $$WATCHERS ARE WHAT DRIVES ANGULAR
  9. < i n p u t n g - m

    o d e l = " t i t l e " > < d i v > I n p u t v a l u e : < s p a n > { { t i t l e } } < / s p a n > < / d i v >
  10. NGMODEL INTERNALS v a r i n p u t

    = d o c u m e n t . q u e r y S e l e c t o r ( ' i n p u t ' ) ; i n p u t . a d d E v e n t L i s t e n e r ( ' i n p u t ' , f u n c t i o n ( ) { $ s c o p e . $ a p p l y ( f u n c t i o n ( ) { $ s c o p e . t i t l e = t h i s . v a l u e ; } ) ; } , f a l s e ) ;
  11. NGMODEL CREATES A $$WATCHER $ s c o p e

    . $ w a t c h ( ' t i t l e ' , f u n c t i o n ( n e w V a l u e , o l d V a l u e ) { d o c u m e n t . q u e r y S e l e c t o r ( ' i n p u t ' ) . v a l u e = n e w V a l u e ; } ) ; INTERPOLATION $$WATCHER $ s c o p e . $ w a t c h ( ' t i t l e ' , f u n c t i o n ( n e w V a l u e , o l d V a l u e ) { d o c u m e n t . q u e r y S e l e c t o r ( ' s p a n ' ) . i n n e r H T M L = n e w V a l u e ; } ) ;
  12. S c o p e { $ $ w a

    t c h e r s : [ { e q : f a l s e , e x p : f u n c t i o n ( ) { } , f n : f u n c t i o n ( n e w V a l u e , o l d V a l u e ) { d o c u m e n t . q u e r y S e l e c t o r ( ' i n p u t ' ) . v a l u e = n e w V a l u e ; } , g e t : f u n c t i o n ( ) { r e t u r n $ s c o p e . t i t l e ; } , l a s t : ' ' } , { e q : f a l s e , e x p : f u n c t i o n ( ) { } , f n : f u n c t i o n ( n e w V a l u e , o l d V a l u e ) { d o c u m e n t . q u e r y S e l e c t o r ( ' s p a n ' ) . i n n e r H T M L = n e w V a l u e ; } , g e t : f u n c t i o n ( ) { r e t u r n $ s c o p e . t i t l e ; } , l a s t : ' ' } ] }
  13. $digest cycle commences $scope.$apply is executed $scope.title is updated with

    input.value Angular app User types inside <input>
  14. Continues to next $$watcher until “clean ” If di erent,

    execute the “fn() ” property Re-runs $digest to ensure all $scopes are synced Compares the value to the “last ” property $digest loop Runs the “get() ” function to retrieve latest value
  15. SOLUTION: CONSIDER NG-BIND < ! - - b e f

    o r e - - > < p > H e l l o { { n a m e } } a n d w e l c o m e t o F a c e b o o k ! < / p > < ! - - a f t e r - - > < p > H e l l o < s p a n n g - b i n d = " n a m e " > < / s p a n > a n d w e l c o m e t o F a c e b o o k ! < / p >
  16. SOLUTION: NGMODELOPTIONS < i n p u t t y

    p e = " t e x t " n g - m o d e l = " v m . m o d e l " n g - m o d e l - o p t i o n s = " { u p d a t e O n : ' d e f a u l t b l u r ' , d e b o u n c e : { ' d e f a u l t ' : 2 5 0 , ' b l u r ' : 0 } } " >
  17. SOLUTION: ONE-TIME BINDINGS < u l > < l i

    n g - r e p e a t = " i t e m i n : : i t e m s " > { { i t e m } } < / l i > < / u l > < u l > < l i n g - r e p e a t = " i t e m i n i t e m s " > { { : : i t e m } } < / l i > < / u l >
  18. DOM rendering is expensive Huge DOM rendering is even more

    expensive We don't always need to re-render EXCESSIVE DOM RE-RENDERING
  19. SOLUTION: TRACK BY < u l > < l i

    n g - r e p e a t = " i t e m i n i t e m s t r a c k b y i t e m . i d " > { { i t e m } } < / l i > < / u l >
  20. SOLUTION: CONTROLLER FILTERING f u n c t i o

    n M a i n C o n t r o l l e r ( $ f i l t e r ) { v a r c o l l e c t i o n = [ { . . . } ] ; t h i s . f i l t e r e d = c o l l e c t i o n ; t h i s . o n C l i c k = f u n c t i o n ( p r o p ) { / / D O M e q u i v a l e n t : c o l l e c t i o n | m y F i l t e r : p r o p t h i s . f i l t e r e d = $ f i l t e r ( ' m y F i l t e r ' ) ( c o l l e c t i o n , p r o p ) ; } } a n g u l a r . m o d u l e ( ' a p p ' ) . c o n t r o l l e r ( ' M a i n C o n t r o l l e r ' , M a i n C o n t r o l l e r ) ;
  21. SOLUTION: $HTTP + $APPLYASYNC f u n c t i

    o n c o n f i g ( $ h t t p P r o v i d e r ) { $ h t t p P r o v i d e r . u s e A p p l y A s y n c ( t r u e ) ; } a n g u l a r . m o d u l e ( ' a p p ' , [ ] ) . c o n f i g ( c o n f i g ) ;
  22. SOLUTION: STRICT DI MODE / / u s a g

    e : < h t m l n g - s t r i c t - d i > f u n c t i o n S o m e S e r v i c e ( $ s c o p e , $ t i m e o u t ) { / / . . . } / / A r r a y a n n o t a t i o n s S o m e S e r v i c e . $ i n j e c t = [ ' $ s c o p e ' , ' $ t i m e o u t ' ] ; a n g u l a r . m o d u l e ( ' a p p ' ) / / I n l i n e a n n o t a t i o n s . f a c t o r y ( ' S o m e S e r v i c e ' , [ ' $ s c o p e ' , ' $ t i m e o u t ' , S o m e S e r v i c e ] ) ;
  23. SOLUTION: UNDERSTANDING < ! - - n g - i

    f / n g - s w i t c h - - > < u l n g - i f = " v m . e x p o s e N a v " > < l i n g - r e p e a t = " m e n u i n v m . m e n u s " > < / l i > < / u l > < ! - - n g - s h o w / n g - h i d e - - > < u l n g - s h o w = " v m . e x p o s e N a v " > < l i n g - r e p e a t = " m e n u i n v m . m e n u s " > < / l i > < / u l >
  24. SOLUTION: DISABLE DEBUG INFO f u n c t i

    o n c o n f i g ( $ c o m p i l e P r o v i d e r ) { $ c o m p i l e P r o v i d e r . d e b u g I n f o E n a b l e d ( f a l s e ) ; } a n g u l a r . m o d u l e ( ' a p p ' , [ ] ) . c o n f i g ( c o n f i g ) ;
  25. SOLUTION: DISABLE DEBUG INFO < ! - - e n

    a b l e d - - > < d i v n g - c o n t r o l l e r = " M a i n C t r l a s v m " c l a s s = " n g - s c o p e n g - b i n d i n g " > < m y - d i r e c t i v e c l a s s = " n g - i s o l a t e - s c o p e " > < / m y - d i r e c t i v e > < / d i v > < ! - - d i s a b l e d - - > < d i v n g - c o n t r o l l e r = " M a i n C t r l a s v m " > < m y - d i r e c t i v e > < / m y - d i r e c t i v e > < / d i v >