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

Angular Performance at Angular Beers in Barcelona

Angular Performance at Angular Beers in Barcelona

Manfred Steyer

October 17, 2017
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. About me… • Manfred Steyer • SOFTWAREarchitekt.at • Trainer &

    Consultant • Focus: Angular • Google Developer Expert (GDE) Page ▪ 2 Manfred Steyer
  2. Contents • Lazy Loading and Preloading • Performance for Data

    Binding with OnPush • AOT and Tree Shaking • Caching with Service Worker
  3. Module Struktur Page ▪ 8 AppModule … … … SharedModule

    Root Module Feature Modules Shared Module
  4. Lazy Loading Page ▪ 9 AppModule … … … SharedModule

    Root Module Feature Modules Shared Module
  5. Root Module with Lazy Loading Page ▪ 10 const APP_ROUTE_CONFIG:

    Routes = [ { path: 'home', component: HomeComponent }, { path: 'flights', loadChildren: './[…]flight-booking.module#FlightBookingModule' } ];
  6. Routes for "lazy" Module Page ▪ 11 const FLIGHT_ROUTES =

    [ { path: '', component: FlightBookingComponent, […] }, […] }
  7. Routes for "lazy" Module Page ▪ 12 const FLIGHT_ROUTES =

    [ { path: 'subroute', component: FlightBookingComponent, […] }, […] } flight-booking/subroute Triggers Lazy Loading w/ loadChildren
  8. Lazy Loading • Lazy Loading means: Loading it later •

    Better startup performance • Delay during execution for loading on demand
  9. Idea • Module that might be needed later are loaded

    after the application started • When module is needed it is available immediately Page ▪ 16
  10. OnPush flights flight flight {{ flight.id }} {{ flight.id }}

    FlightSearch Card Card Angular just checks when “notified”
  11. "Notify" about change? • Change bound data (@Input) • Angular

    compares just the object reference! • e. g. oldFlight === newFlight • Raise Event within the component • Notify a bound observable • Trigger it manually • Don't do this at home ;-) • At least: Try to avoid this
  12. Change Inputs flights flight flight {{ flight.id }} {{ flight.id

    }} FlightSearch Card Card flightold === flightnew
  13. Not „all or nothing“ • You can use OnPush just

    for selected components • Using it compleatly: Redux • @ngrx/store
  14. Advantages of AOT • Better Startup-Performance • Smaller Bundles: You

    don't need to include the compiler! • Tools can easier analyse the code • Remove not needed parts of frameworks • Tree Shaking
  15. Angular CLI • ng build --prod • @ngtools/webpack with AotPlugin

    • Soon AngularCompilerPlugin • Can be used without CLI too
  16. Challenges • Most tree shaking tools are conservative • They

    just remove code when they are 100% sure • Very often, they aren't sure :-) • Solution: Angular Build Optimizer • Rewrites compiled code • Currently: Experimental
  17. What are Service Workers? • Background Tasks • Web App

    installs them • Are activated and deactivated on demand
  18. Service Worker und Caching/ Offline • Intercept requests • Decide

    how to respond (Cache, Network) • Same Origin Policy • Caching Patterns • Cache only • Network only • Try cache first, then network • Try network first, then cache • etc.
  19. Service Worker with Workbox (sw.js) importScripts('./assets/workbox-sw.js'); const workboxSW = new

    WorkboxSW(); const networkFirst = workboxSW.strategies.networkFirst(); const cacheFirst = workboxSW.strategies.cacheFirst();
  20. Service Worker with Workbox (sw.js) importScripts('./assets/workbox-sw.js'); const workboxSW = new

    WorkboxSW(); const networkFirst = workboxSW.strategies.networkFirst(); const cacheFirst = workboxSW.strategies.cacheFirst(); workboxSW.router.registerRoute( new RegExp('^http:\/\/www.angular.at\/api\/'), networkFirst); workboxSW.router.registerRoute(/./, cacheFirst);
  21. More about this in my Medium Account • Configuration Details,

    Samples etc. • https://medium.com/@ManfredSteyer/angular-performance-tuning- article-series-6e3c33707b25
  22. Conclusion Quick Wins Lazy Loading and Preloading AOT and Tree

    Shaking Caching w/ Service Worker OnPush w/ Immutables and Observables