7 min read

Common Web Performance Optimizations

Table of Contents

Network Optimizations

Frontend-Backend-Work

Improve network performance:

Critical Render Path

Critical render path diagram
  • In the critical render path, there are 8 steps, the very first step is the download of the HTML file
  • the browser starts creating the DOM, and incrementally generates the tree model of the html tags within the file
  • DOM describes the contents of the page
  • just when it starts to do that, it gets the style link, so it fetches the CSS file
  • this starts the simultaneous creation of the CSSOM (CSS Object Model) which has the styling information attached to the tree nodes, in a similar fashion to the DOM
  • then, slowly it traverses down the html file and sees the script tag with a link to a js file, so it grabs that
  • the JS file is read by the browser and executes any changes requested within the DOM and the CSSOM
  • once that is done, the browser combines the DOM and the CSSOM into a render tree which has information of both, the html, and the styling
  • it uses the render tree to create the layout based on the files and paint the layout and pixels onto the screen
  • thus, we finally have our web-page
    • btw, any images and other types of files needed are also fetched in the background and are not part of this process

Improving Step 1 of CRP: Streaming of the HTML file to the browser

  • start loading CSS as early as possible
  • load JS as late as possible (few exceptions like google tag to track what a user is doing as early as site load). Normally if you put it earlier than script tags at the bottom, it will block the render and other resources being downloaded
  • JS requires HTML and CSS parsing to be done before it can begin its work (step 4)

Improving Step 2 of CRP: Streaming of the CSS file to the browser

  • CSS is render blocking (can’t create render tree before we have the CSS to create CSSOM)
  • make CSS as lightweight as possible
  • only load whatever is needed
  • above the fold loading
    • only load what’s on the main page Above the fold loading
  • media attributes
    • can use it to load stylesheets too
    • link tags have a hidden media attribute which defaults to all
    • we can change it to media="only screen and (min-width:500px)" for a particular stylesheet to be
  • less specificity
    • good:
      a.important {
        color: pink;
      }
      
    • bad:
      .header .nav .item .link a.important {
        color: pink;
      }
      

Improving Step 3 of CRP: Streaming of the JS file to the browser

  • load scripts async’ly Script-tag-async-defer-comparison
  • <script async>
  • go ahead dl the js file with another thread
  • non blocking
  • when it’s done dl’ing, it immediately executes js which might be bad for us:
    • if it executes long after the page load, then if we are relying on the JS for changes to UX, we’ll provide bad UX to user
    • if executes before page load, we can’t do any DOM manipulation resulting in error
  • good for scripts that don’t manipulate the DOM - like tracking or analytics
  • <script defer>
  • wait to execute AFTER our html is parsed
  • good for scripts that will act on the DOM
  • minimize DOM manipulations all together
    • this will block from the CRP to moving to the next step → creating the render tree
    • if you have one or more js files in script tags, sync tags will block other js files to be loaded if there are DOM manipulations ongoing in the first script
  • avoid long running javascript!
    • avoid alerts and random blocking JS code

Improving Step 4 of CRP: Steps 4-8

  • Render tree enables the layout and position then paints the elements
  • Step 8 is if JS events change any part of the page, the browser revisits the render tree, redraws and re paints again
  • modern browsers are smart enough for partial re-draws, but try to limit

Test webpage performance

Other Resources

http://optimizilla.com/https://tools.pingdom.com/

https://www.thinkwithgoogle.com/feature/mobile/

https://developers.google.com/web/tools/lighthouse/

http://websitespeedranker.com/

https://pageweight.imgix.com/

https://developers.google.com/speed/pagespeed/insights/

https://passmarked.com/

https://images.guide/https://www.crazyegg.com/blog/image-editing-tools/

Additional image manipulation tools:

  • XNConvert: This free, cross-platform tool can handle batched images, and performs resizing, optimization, and other transforms.
  • ImageOptim: This free tool is available for Mac and as an online service, and is specifically aimed at optimizing images for speed, including metadata removal (discussed above).
  • ResizeIt: A Mac-only desktop product that lets you change the size of multiple images simultaneously, and can convert file formats at the same time.
  • PicResize: One of several good browser-based tools that gives you lots of options for cropping, rotating, resizing, adding effects to, and converting images.
  • Gimp: This ever-popular cross-platform tool just gets better with age. Powerful and flexible, Gimp lets you perform a wide variety of image manipulation tasks including, of course, resizing.

Other optimizations and resources