Pino bio photo


I (infrequently) blog about data science, business intelligence, big data, web technologies and free software.

Twitter LinkedIn

I was prototyping a new web product a few days ago and I wanted to enhace customers’ mobile experience. The results from the Google PageSpeed test were pretty alarming: my prototype was very slow. I was determined to improve the website speed, but I couldn’t afford to spend too much time on it since it’s in the prototyping phase.

In less than an hour I was able to take the PageSpeed score from 41 to 80 by tracking the most severe (and basic) problems. All serious websites should have these three problems covered in a way. If you are unsure if your website has it covered, just use PageSpeed.

Google PageSpeed test results

Lossy compression of images

We all know that one of the main reasons why many websites load slow are the images. I’ve got very good results by just using two simple command lines tools: pngquant and jpegoptim.

pngquant --quality 20 *.png
$ jpegoptim -m20 *.jpg

Expirty date in HTTP headers

This is what Google says regarding having a expiry date in the HTTP headers:

Setting an expiry date or a maximum age in the HTTP headers for static resources instructs the browser to load previously downloaded resources from local disk rather than over the network.

Fixing this problem can take you literally seconds if you are using apache webserver. First, you have to activate mod_expires:

$ sudo a2enmod expires

Second and last, add a rule to make all static files expire after one month. In my case it was very easy since I have a different directory configured to server them:

<Directory /var/www/FlaskApp/FlaskApp/templates/static/>
  Order allow,deny
  Allow from all
  <IfModule mod_expires.c>
      ExpiresActive on
      ExpiresDefault	"access plus 1 month"

Defer loading

Reusing examples from StackOverflow to defer load JavaScript and another one to defer load CSS, I ended up with the following code:

var dfLoadStatus = 0;
var dfLoadFiles = [

function loadStyleSheet(src){
   if (document.createStyleSheet) document.createStyleSheet(src);
   else {
       var stylesheet = document.createElement('link');
       stylesheet.href = src;
       stylesheet.rel = 'stylesheet';
       stylesheet.type = 'text/css';

function deferLoading(){



function downloadJSAtOnload() {
    if (!dfLoadFiles.length) return;

    var dfGroup = dfLoadFiles.shift();
    dfLoadStatus = 0;

    for(var i = 0; i<dfGroup.length; i++) {
        var element = document.createElement('script');
        element.src = dfGroup[i];
        element.onload = element.onreadystatechange = function() {
        if ( ! this.readyState ||
               this.readyState == 'complete') {
            if (dfLoadStatus==0) downloadJSAtOnload();


if (window.addEventListener)
    window.addEventListener("load", deferLoading, false);
else if (window.attachEvent)
    window.attachEvent("onload", deferLoading);
else window.onload = deferLoading;

Remember to only do refer load with those resources that are not essential and to load the resources in the right order!