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

Angular Performance - State of Affairs 2018 -- ...

Angular Performance - State of Affairs 2018 -- Slides from JAX 2018, April 2018 in Mainz

Avatar for Manfred Steyer

Manfred Steyer

April 25, 2018
Tweet

More Decks by Manfred Steyer

Other Decks in Programming

Transcript

  1. @ManfredSteyer About me… • Manfred Steyer • SOFTWAREarchitekt.at • Angular

    Trainings and Consultancy (Public and In-House) • Google Developer Expert (GDE) Page ▪ 3 Manfred Steyer
  2. @ManfredSteyer Contents • Lazy Loading • Performance for Data Binding

    with OnPush • AOT and Tree Shaking • New and upcoming features • Caching with Service Worker • Serverside Rendering
  3. @ManfredSteyer Module Structure Page ▪ 8 AppModule … … …

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

    SharedModule Root Module Feature Modules Shared Module
  5. @ManfredSteyer Root Module with Lazy Loading Page ▪ 10 const

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

    = [ { path: '', component: FlightBookingComponent, […] }, […] }
  7. @ManfredSteyer Idea • Module that might be needed later are

    loaded after the application started • When module is needed it is available immediately Page ▪ 16
  8. @ManfredSteyer Activate Preloading Page ▪ 17 … imports: [ […]

    RouterModule.forRoot( ROUTE_CONFIG, { preloadingStrategy: PreloadAllModules }); ] …
  9. @ManfredSteyer OnPush flights flight flight {{ flight.id }} {{ flight.id

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

    OnPush: Angular just compares the object reference! • e. g. oldFlight === newFlight • Raise Event within the component • Notify a bound observable • observable$ | async • Trigger it manually • Don't do this at home ;-) • At least: Try to avoid this
  11. @ManfredSteyer Advantages of AOT • Better Startup-Performance • Smaller Bundles:

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

    AngularCompilerPlugin • Can be used without CLI too
  13. @ManfredSteyer Getting rid of zone.js (since Angular 5) • Consequence:

    You need to do change detection manually • Not fun ;-) Don't do this (carelessly)! • Ideas to solve this, e. g. observable$ | somePipe • Credits to Jeremy Wilken from the Angular Universal Team • Dreams of the (near) future? platformBrowserDynamic().bootstrapModule(AppModule, { ngZone: 'noop' })
  14. @ManfredSteyer Solutions Angular Language Service • Plugins for IDEs AOT

    for debugging • Possible since Angular 5 • Incremental AOT • Fast after 1st recompilation • ng serve --aot • Give it a try!
  15. @ManfredSteyer Traditional Providers are not Treeshakable! @NgModule({ […], providers: [

    LoggerService, { provide: LogFormatterService, useClass: AdvancedLoggerFormatterService } ], […] }) export class AppModule { }
  16. @ManfredSteyer Further improvements in upcoming versions New View Engine ngIvy:

    More treeshakable Code No Breaking Changes "Early numbers are extremely promising in terms of code size" -- Rob Wormald Experimental with Angular 6
  17. @ManfredSteyer // GENERATED CODE (you don't write this) viewDef([ elementDef(0,

    'div', ['class', 'greeting'], null, ...); textDef('Hello, world!', ...); ]); <div class="greeting"> Hello, world! </div> [Source: Green, Erickson, Hevery: ngconf 2018 Keynote, https://tinyurl.com/y7t4sfzu]
  18. 25 Kb 20 Kb 10 Kb 5 Kb 0 30

    Kb 10 Kb 7.3 Kb 2.7 Kb Cake Goal Minified Compressed Cake Threshold 36 Kb Current (compressed) (no-zone.js) Hello World [Source: Green, Erickson, Hevery: ngconf 2018 Keynote, https://tinyurl.com/y7t4sfzu]
  19. @ManfredSteyer Sample class Stuff { static stuff(data: string) { console.log('stuff:

    ' + data); } } function stuff(data: string) { Stuff.stuff(data); } stuff('Hallo Welt!'); console.log("stuff: Hallo Welt!");
  20. @ManfredSteyer ABC • Angular • Bazel = Build Tool •

    Closure = Optimizing Compiler • Angular Labs Project == Experimental
  21. @ManfredSteyer Removing Whitespaces <p> Hello</p> <p>World! </p> \n //Pseudo Code

    createNode('p', ' Hello World'); createNode('TEXT-NODE', '\n'); createNode('p', 'World!'); Template Compiler
  22. @ManfredSteyer Removing Whitespaces <p> Hello</p> <p>World! </p> \n //Pseudo Code

    createNode('p', ' Hello World'); createNode('TEXT-NODE', '\n'); createNode('p', 'World!'); Template Compiler
  23. @ManfredSteyer Challenges Other conditions Separate Services for Server and Client-Seite

    Renderer abstracts DOM 3rd parts libs Angular 5: Server Side DOM Simulation (partly)
  24. @ManfredSteyer Conclusion Quick Wins Lazy Loading and Preloading OnPush w/

    Immutables and Observables AOT and Tree Shaking Treeshakable Providers ngIvy Pipeable Rx-Operators Server Side Rendering
  25. @ManfredSteyer More about this in my Medium Account • Configuration

    Details, Samples, Videos etc. • https://medium.com/@ManfredSteyer/angular-performance-tuning- article-series-6e3c33707b25