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

Web Apps performance & JavaScript compilers

Web Apps performance & JavaScript compilers

MostJS Frameworks Day @ Dec 4 2016

Roman Liutikov

December 04, 2016
Tweet

More Decks by Roman Liutikov

Other Decks in Programming

Transcript

  1. This is NOT good • Makes people wait • Eats

    their traffic and money • Everyone hates you • You loose money
  2. The right way _______ index.html route-1.js common-1.js I can see

    it I can use it route-2.js Pre-caching Server Rendering (JS & CSS)
  3. Code splitting Divide JS bundle into chunks route-1.js route-2.js route-3.js

    route-4.js ... common-1.js common-2.js Webpack CommonsChunkPlugin
  4. Dead code elimination Remove code that can never be executed

    const dec = (x) => x - 1; const inc = (x) => x + 1; const x = 0; const y = 1; inc(x); inc dec y x UglifyJS Rollup Babili Closure Compiler Dependency graph
  5. Tree-shaking (A special case of DCE) Do not include unused

    exports // math.js export const dec = (x) => x - 1; export const inc = (x) => x + 1; // main.js import { inc } from 'math'; const x = 0; const y = 1; inc(x); Webpack 2 Rollup Closure Compiler
  6. Function call inlining Replace a function call with its body

    const person = { fname: 'John', lname: 'Doe' }; function fullName(p) { return p.fname + ' ' + p.lname; } console.log(fullName(person)); Closure Compiler const person = { fname: 'John', lname: 'Doe' }; console.log( person.fname + ' ' + person.lname);
  7. Property flattening (collapsing) Collapse object properties into separate variables const

    person = { fname: 'John', lname: 'Doe' }; console.log( person.fname + ' ' + person.lname); Closure Compiler const person$fname = 'John'; const person$lname = 'Doe'; console.log( person$fname + ' ' + person$lname);
  8. Constant folding Evaluate constant expressions at compile time UglifyJS Babili

    Closure Compiler const person$fname = 'John'; const person$lname = 'Doe'; console.log('John Doe'); const person$fname = 'John'; const person$lname = 'Doe'; console.log( person$fname + ' ' + person$lname);
  9. Known methods folding Evaluate known methods at compile time Closure

    Compiler '012345' 'World!' 99 [0, 1, 2, 3, 4, 5].join(''); 'Hello, world!'.substr(7, 11); parseInt('99 UAH');
  10. Code splitting & Lazy loading in Webpack 2 Per route

    chunks with React Router <Route path="user/:id" getComponent={(loc, cb) => { System.import("pages/User") .then((module) => cb(null, module)); }}>
  11. Preload chunks * Chrome, Opera & Chrome for Android <head>

    <link rel="preload" as="script" href="chunk-1.js"> <link rel="preload" as="script" href="chunk-2.js"> ... </head>
  12. Precache with Service Worker (sw-precache) Preload + Offline support plugins:

    [ new SWPrecacheWebpackPlugin({ cacheId: "app", filename: "app-sw.js", staticFileGlobs: [ "app/js/**.js" ] })]