paint-brush
Loaded in 600 Milliseconds: How To Improve Website Speedā€‚by@veedran
1,356 reads
1,356 reads

Loaded in 600 Milliseconds: How To Improve Website Speed

by Vedran Cindric 12mAugust 27th, 2021
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

The goal is to bring your website performance up as fast as possible. 90% of the time, the content we are loading on the rest of the website is images and videos. Lazy loading is probably the simplest and most effective thing you can do to improve your website load time. The first way we can add lazy loading to our website is by using a really tiny Javascript library called: lazysizes. You can lazy load images, iframes, videos, and even widgets which is great for responsive design.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Loaded in 600 Milliseconds: How To Improve Website Speed
Vedran Cindric  HackerNoon profile picture

One of the first movies I ever saw in a cinema was in my tiny home townĀ Nova GradiÅ”kaĀ in Croatia. The movie playing that day wasĀ Gone in 60 Seconds. As you can imagine, I was blown away. My first movie on the big screen. And at the time there were a lot of popular actors in it like Nicolas Cage, Angelina Jolie, Vinnie Jones, and others. It really was an experience I remember to this day. In particular, a scene from that movie seems like a great intro to this article. So, before you continue, play the video andĀ get yourself into the performance groove. Don't forget to do the finger thing that Cage does šŸ˜‚

Before you think "Oh no, yet another guide I'll never actually be able to implement on my website" let me stop you there!Ā More or less everything we will go through in this article is easily achievable.Ā In some cases, some things might take 2 minutes and some might take longer but in general, the goal is to bring your website performance up as fast as possible.

Personally, I'm very passionate about website performance, loading times, and optimizing everything in the stack to run as smoothly as possible. For the past 10 years, I've spent a lot of my time chasing milliseconds in our software development company. Performance is something we included in every one of our projects and the same is with our startup -Ā Treblle. Besides that, Iā€™ve also spent a lot of time consulting other companies on how to improve performance. Going after their developers and tidying things up.

Based on my experience I came up with a list of things you can do almost day one on any projects that would improve the speed drastically and show a noticeable improvement. Some will take time but most are hassle-free.

Lazyload (everything)

When someone visits your website they probably don't see the entire website but only, what in the marketing world they call, "above the fold" content.

In most cases that includes a hero element with either text, images/video, and a call to action. Below that is everything else explaining why your product or startup is the next big thing (same as ours šŸ˜Ž). More often than not a user might only view that hero element, scroll a few times but wonā€™t reach the end of the website.Ā 

So why load all the images, videos, iframes, and content that nobody will see?Ā That right there is the fundamental idea behind lazy loading. Itā€™s probably the simplest and most effective thing you can do to improve your website load time. The reason why it has such a huge impact is that 90% of the time, the content we are loading on the rest of the website is images and videos. So if you have 20 - 30 images on your website and the users can only see 3-4, why load the rest? Sounds logical - it is - but you would be surprised at how many websites don't do this. Especially considering that itā€™s super easy to add it to your website.

The first way we can add lazy loading to our website is by using a really tiny Javascript library called:Ā lazysizes. I've used this puppy in every website weā€™ve developed for the past 5 years and it works like a charm. You can lazy load images, iframes, videos, and even widgets. It works with the

Ā <img>Ā 
element as well as picture elements which is great for responsive design.

To use it simply replace your existingĀ 

<img>
Ā elements with the following:

<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="path-to-my-image.jpg" class="lazyload" alt="Lazy loaded image">


Note that the path to the real image is in the "data-src" attribute and to enable lazy loading you have to add a class of "lazyload". You also might be wondering what the mambo jumbo in the "src" attribute is. Well, itā€™s basically a really tiny image with a light gray background. This is used to fill up the space and not cause jumps in layouts shift while the image is "replaced". In any case, you are done. Nothing more or less to do. The plugin has more options that you can check out on their website.

Another way you could start using lazy loading is by utilizing a new HTML standard attribute calledĀ 

loading
. Now it does have limited support but it is probably the future of lazy loading and it will be a default HTML attribute once everybody starts supporting it.

You use it by addingĀ 

loading="lazy"
Ā to any image element like so:

<img src="image.png" loading="lazy" alt="Lazyloaded image" width="200" height="200">

Again, maybe wait a bit with this one because itā€™s lacking support for older browsers but definitely keep an eye for it.

Optimize images

I'm adding this because it has a huge impact and itā€™s dead simple to do. Yet - most people donā€™t do it. When you are using images as design elements, backgrounds, and such simply run them through something likeĀ ImageOptimĀ orĀ JPEGMini.

I've been using the FREE Mac app ofĀ ImageOptimĀ for years and it works like a charm. In some cases, itĀ reduces image sizes by like 80%Ā and I simply get shocked. You don't have to think about it, simply drag and drop the images and they are automatically saved as optimized images with as little as possible quality loss. They also have a web service so you can connect your user-generated images with their API and have them optimized as well.

As far as SVG goes I use another free and awesome service calledĀ SVG Nano. It's just likeĀ ImageOptimĀ but for SVGs and the results are astonishing. You can have a Full HD illustration cut down to like 10KB which is great.

Minify and compress

A must-do when working with CSS and JS is to minimize them! Don't use a non minimized version of your code or other plugins/packages/dependencies in production. Simply don't. You can manually minimize the CSS/JS via tools like this: https://minifycode.com/ or https://www.minifier.org/.

Ideally, you should include minification in your build process either viaĀ WebpackĀ or some other tool. If you don't know how to do any of those then you can maybe download already minified versions of various frameworks and JS plugins.

Just consider this: a non minified and uncompressed version of Jquery is 290Kb whereas a minified and compressed version is 90KB.Ā You just saved 200KB or 70% of the download size!

Preconnect

Preconnect is a relatively modern type of link element that became popular with HTML5 and HTTP2. The idea behind it is super simple but very effective. By adding one or more preconnect tags to theĀ 

<head>
Ā element of your website you tell browsers that, down the line, you will eventually download something from that domain. Browsers can initiate a connection and essentially prepare whatever they need to instantly download something from that domain.

This, as you can imagine, can speed things up for you - especially if you are downloading fonts, images, and JS. Using preconnect could not be simpler. Before you call on any CSS and JS in yourĀ 

<head>
Ā element add one of these:

<link rel="preconnect" href="https://domain.com/" crossorigin>

And that's it. Later on, if you're downloading something from domain.com like an image or JS or CSS it will be faster because the browser is already ready for it. To give you an idea here are the top 3 most common preconnect scenarios:

<link rel="preconnect" href="https://fonts.gstatic.com/" crossorigin>
<link rel="preconnect" href="https://www.google-analytics.com/" crossorigin>
<link rel="dns-prefetch" href="https://yourid.cloudfront.net">

Use a CDN

Today with cloud providers like AWS, Azure, and CloudFlare using a CDN couldn't be easier and it has a big performance impact! A CDN or a Content Distribution Network simply takes time files you upload (CSS/JS/images) and stores them across multiple different servers in various parts of the world. So let's say you have a visitor from India and your server is in the Us. If you are using a CDN for your assets then that CDN will serve those assets from a server in India rather than the US one.Ā This will reduce latency and load times but also enable parallel resource downloadingĀ since the files are downloaded from a different domain.

Another great thing about CDNs is that they are already optimized for performance. Everything we had to do to tweak our server earlier is already done for us šŸ¤ŸšŸ» If you are worried about pricing - don't be. Using a CDN nowadays is super affordable especially for the impact it has on performance.

Go back to basics

I've always been a fan of minimalism except when it comes to Apple devices šŸ„ŗ Guilty as charged. But when it comes to front-end development I love minimalism to a fault. What I mean by minimalism is simple: reduce the number of resources you are using in your pages. This time under resources I don't just mean images and JS but rather fonts, various CSS frameworks, JS frameworks, and dependencies.Ā When we are talking about fonts my rule is 1 custom font with a maximum of 2-3 weights. If I really really have to, I will include a second font but then I will try to reduce the weights as much as possible. And yes I understand you should make your website look pretty but not at the cost of extreme performance.

Another really trivial and stupid problem I repeatedly have seen happen is that developers include multiple versions of JQuery or Bootstrap or some other plugin. Iā€™ve noticed this goes to extremes with WordPress themes and plugins. Itā€™s like a frenzy of repeat includes. Every plugin simply piles on its own Bootstrap version, Jquery version, or something. Make sure you nip that because you really donā€™t wanna be loading multiple useless libraries.

Finally, think about what you actually need. Do you need a 150KB animation library for CSS and animating that one section on the home page? Can that be done using native CSS or JS? Do you really need JQuery, an Ajax Form plugin, and a Form validation plugin to make an HTTP request? Do you need a 200KB parallax plugin that requires another dependency or can you make a similar effect with CSS? Do you really need another library to tell time with JS?

Consider the fact that an average modern F1 car has 5000 parts and weighs around 750 KG whereas a regular car has around 30 000 parts and weights 1300 KG. Just let that sink in.Ā Do you want your website to be like an F1 car or like your neighbors Toyota Camry?

Optimize your server

Your infrastructure has a significant role in load times and performance. If you have a lousy server nothing you do will help you. Especially if the server is located in a different part of the world and most of your users are accessing it from across the pond. There are easy wins that you can do to make sure you get the best performance with as little as possible.

The first thing is to make sure that you are running on HTTP2. It's not that complicated to install it in case your hosting provider isn't running on that by default. HTTP2 is much faster than HTTP1. It's also much newer so it has a lot more features that deal with a lot of performance issues that HTTP1 had. One of the biggest things that HTTP2 can do is download resources in parallel. Coupled with other features HTTP2 makes a huge difference and you should find a way to get your website on it.

Next up if you are developing on top of the LAMP stack and you are using PHP then go check if you are running PHP 7+ at a minimum. PHP has been kicking ass in terms of performance in recent versions like PHP 7 and PHP 8. Just by upgrading from PHP 5 to 7 you will get 60% better performance and render times. PHP 8 also contains a lot of JIT improvements which again makes the jump from 7 to 8 worth it.

Another thing you can easily do is adjust the server configuration to make it even faster. This includes making sure you are compressing resources like CSS/JS, sending the correct mime types, taking advantage of browser cache, and others. To save you some time I'll share my custom Apache configuration I've been using for years.

The first step would be to create a new Apache config with our performance settings. Open your terminal, SSH to your server, and navigate to

/etc/apache2/conf-available/
Ā and type in:

sudo curl https://gist.githubusercontent.com/cindreta/e536faa5494d0a0be409d435757ff9a7/raw/0a24629059e243a44d2caf5d1e95ce1c8df4822f/performance.conf - o performance.conf

This will download the custom performance configuration from a gist on my Github and save it as a file calledĀ 

performance.conf
Ā which we will include in our default Apache config.

Now that we have the file navigate toĀ 

/etc/apache2/sites-available/
Ā where you should find all your Apache Vhosts. The default one should be calledĀ 
000-default.conf.
Simply do sudo nano 000-default.conf and before the end tagĀ 
</VirutalHost>
Ā simply add our custom config like so:

Include /etc/apache2/conf-available/performance.conf

Save the changes and reload the Apache server by issuing a command:Ā 

sudo systemctl restart apache2.
If apache starts normally then everything is ok, if we messed something up or you are missing some Apache modules like mod_deflate, mod_rewrite apache will throw an error and you can either try to debug whatā€™s up or remove the config file from your vhost.

An additional note I would like to mention: you could have used .htaccess to enable the configuration but I would suggest you don't. The reason is, you guessed it, performance. Adding complicated and long config options toĀ 

.htaccess
Ā actually slows down the Apache server because it has to do more work to compile that. I donā€™t like messing around with servers also but itā€™s worth doing it via config files like shown.

Understand HTML

HTML is the most forgiving language on planet Earth. If you make a mistake like forgetting to close a tag or mistype the tag name it will still do its best to render and show the content. A lot of this has to do with browser engines but the fact is you can get away with pretty much anything. And people tend to abuse that. There are around 100 tags in HTML spanning from more common likeĀ 

<div>
,Ā 
<p>
,Ā 
<h1>
Ā to weird ones likeĀ 
<cite>
,Ā 
<ins>
,Ā 
<kbd>
Ā and others. Yet people mostly use 10 of them and probably the one that is used the most is theĀ 
<div>
Ā element. I've seen this trending phenomenon 10x with these "new kids on the block" learning HTML via React or Angular...In short, learn HTML and use appropriate tags. If you are building a list then useĀ 
<ul>
Ā orĀ 
<ol>
. If you are displaying titles then useĀ 
<h1>
,Ā 
<h2>
,Ā 
<h3>
... And guess what - the numbers have a meaning šŸ˜Š.Ā <h1>Ā would imply it's the most important title on the page, hence use it accordingly. The same goes for other elements on the page likeĀ 
<nav>
,Ā 
<article>
,
<p>
,
<a>
.

Writing good, semantic, meaningful HTMLĀ helps the browser render fasterĀ but also it saves time on paint events, style recalculations, and similar. Another benefit of writing good structured HTML is the on-site SEO aspect of it. Google and other engines love when you make their job easier and use tags that have meaning. It also helps anyone else reading your source to better understand what you tried to do, what is what, and similar. So just use HTML asĀ Sir TimĀ wanted you to.

Another small but interesting tip is to minimize the amount of white space and empty lines. This helps with performance. You can even do this automatically by using server-side plugins mod_pagespeed for Apache and Ngix. Of course, if you are using a back-end programming language that compiles your HTML into views, like Laravel, then there are packages for that like HTMLMin and others.

CSS first

Just like Donald's thing was "America first", I believe, for us front-end developers, it should be "CSS first". What I mean by that is that if you can make something using CSS rather than using an image or Javascript - then do it. When I see a design my first question is what can I do only with CSS. I evaluate everything from backgrounds, shapes, ornaments, structures and try to figure out if there is a way to do it only with CSS.Ā You will be surprised how many things you can do in CSS once you master it.

I've seen many, many instances where CSS was able to replace a shape or even a background image. So by writing maybe 10 lines of CSS you save 50-100KB in transfer data on those images. The main reason why most websites are slow is that they use too many images or pile on too many JS plugins to do basic things like hovering, animations, and effects. CSS is good at all of those things - you just need to learn how to use it.

Another important practice you can apply is writing better and more optimized CSS. Yes - that is a thing. You can also do your website an injustice if you write sloppy and bad CSS. So let's just name a few things you can do to write better CSS:

  • The lesser amount of lines of CSS you can write
  • The better the shorter your selectors are
  • The better run all the animations on the GPU vs the CPU
  • Stop supporting ancient browsers
  • Use shorthand properties when you can
  • Don't overwrite - learn browser defaults and framework resets
  • Separate media queries into its own files
  • Don't bloat your code with third-party addons/plugins that you don't understand

The sooner you start practicing these things the faster you will see results. The best thing about this is you can directly edit CSS rules in a browser and test it in literary seconds. So get to it.

---

Hope some of these help you reduce your load times and improve your performance in one day's work. Some will take longer but they will also make you a better developer in the long run.Ā At the end of the day, performance is a give and take between design, style and, functionality.Ā You should be walking in the middle of that line for the best of both worlds.