How to Make Your Page Load Faster

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • SurveyJS - Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • WorkOS - The modern identity platform for B2B SaaS
  • critical

    Extract & Inline Critical-path CSS in HTML pages

  • CSSOM tree has to be built before start rendering the page. To build it, the browser has to parse the CSS first. I said parse and not load because your CSS can be inlined (in a tag).

    CSS is render-blocking, which means you can't start rendering the page until it's parsed. But it's not parser-blocking, which means the browser can continue parsing the HTML while the CSS is being downloaded (if it was linked externally).

    The main takeaway here is that the larger your CSS file is the longer it will take to start rendering the page because it's a render-blocking resource.

    Loading JavaScript

    JavaScript is a parser-blocking resource. This means once the browser finds any JavaScript code, it will stop parsing immediately to download and execute the JavaScript. After the JavaScript is executed, the parser can continue parsing the document.

    So JavaScript can delay rendering because the browser can't complete building the DOM until it has parsed the HTML.

    JavaScript has to wait for CSSOM

    To make things even worse, your JavaScript code can't start executing if there's CSS being downloaded. In other words, if you have Optimizing your page load time

    Now we get to the fun part; making our pages load faster.

    Getting into the details of each technique is beyond the scope of this article, but I will give you a quick overview of each one. After that, it should be easier to learn more about each one on your own.

    Only load the used/critical CSS

    Most of the time we load CSS that we don't need in the current page. So, we can split the CSS file into smaller ones and use each one based on what the current page needs.

    A common case we can apply this to is loading some third party CSS code only in the pages that need them. For example, only use the code syntax highlighter CSS library on the pages that display code.

    There's also a well-known technique called critical CSS. It's used to load only the CSS code that you see above the fold (the visible part of the browser on the initial load), and then lazy-load the rest asynchronously. And you can make this even better by inlining the critical CSS and lazy-load the rest externally.

    Loading JavaScript asynchronously

    Two popular attributes to achieve this: defer and async. Using either will not cause the browser to wait until they are downloaded to continue parsing.

    The main difference between them is the time and order of execution.

    Defer scripts start executing after the whole DOM is constructed but before DOMContentLoaded event is fired. This also means you should see the page rendered before executing the script.

    Defer scripts execute in the order they were defined. So if script1 is above script2, script1 will always start executing before script2 even if script2 is loaded before.

    Async scripts will start execute as soon as they are fetched. Async scripts don't wait for the DOM to be constructed, which means they can block the parser if an async script is fetched before the DOM is built.

    Async scripts execute in the order they were fetched. The one that loads first executes first.

    The rule of thumb is to use defer scripts when they depend on the content (DOM) of the page, and use async scripts for things that don't need the DOM such as analytics, etc.

    Minify and compress resources

    There are many build tools that help you minify your CSS and JavaScript. Minification is the process of removing any unnecessary characters for execution like spaces, comments, etc.

    Compressing your resources is done by the server. gzip and brotli and the most popular compression types nowadays.

    Minifying and compressing your resources will make them much smaller, which will improve your loading time.

    Preloading important resources

    The browser fetches the resources as it finds them while parsing the HTML. Sometimes it would be useful if we request these resource as soon as possible even before the parser finds them.

    An example would be having two synchronous external JavaScript files below each other. If we don't use preload, then we have to wait for the first script to load and execute before loading the second one. But with preload we can tell the browser to fetch that second script even while it's parsing the HTML.

    Preloading is also used for fetching other resources like CSS, images, fonts, etc.

    Preloading can be done with . Example:

     rel="preload" href="style.css" as="style">
     rel="preload" href="main.js" as="script">
    
    Enter fullscreen mode Exit fullscreen mode

    Reduce your server response time

    No matter how many optimization you do to your client code, it will not make your page load faster if your server takes a long time to send the requested HTML code.

    Usually this is the least we can control especially if we don't maintain the backend code. But if we do, we can do things like optimizing the database queries (how fast we pull data from our database), using caching, compressing and minifying responses, upgrading the server hardware, etc.

    Now you can learn more

    In this article we barely scratched the surface of loading performance optimizations. But it should be easier now to dive deeper into each one as you should have a full understanding of how the browser loads pages.

    An important part we didn't touch in this article is the tools we can use to measure the performance of the page and the impact of the optimizations we do. Google Chrome provides us with two good tools for this: the performance tab in the devtools and Lighthouse. So I encourage you to check them on your own.

  • SurveyJS

    Open-Source JSON Form Builder to Create Dynamic Forms Right in Your App. With SurveyJS form UI libraries, you can build and style forms in a fully-integrated drag & drop form builder, render them in your JS app, and store form submission data in any backend, inc. PHP, ASP.NET Core, and Node.js.

    SurveyJS logo
NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts