martes, 13 de diciembre de 2016

Front-end performance improvements

Defer loading of javascript

This code should be placed in your HTML just before the </body> tag (near the bottom of your HTML file). I highlighted the name of the external JS file.
<script type="text/javascript">
 function downloadJSAtOnload() {
  var element = document.createElement("script");
  element.src = "defer.js";
  document.body.appendChild(element);
 }
 if (window.addEventListener)
  window.addEventListener("load", downloadJSAtOnload, false);
 else if (window.attachEvent)
  window.attachEvent("onload", downloadJSAtOnload);
 else window.onload = downloadJSAtOnload;
</script>
Reference: https://varvy.com/pagespeed/defer-loading-javascript.html

Loading Javascript Asynchronously

Async allows the script to be downloaded in the background without blocking. Then, the moment it finishes downloading, rendering is blocked and that script executes. Render resumes when the script has executed.
<script async src="foobar.js"></script>
Reference: https://www.keycdn.com/blog/blocking-the-dom/

XHR Eval

In this technique, an XMLHttpRequest (XHR) retrieves the JavaScript from the server. When the response is complete, the content is executed using the eval command.
var xhrObj = getXHRObject();
xhrObj.onreadystatechange =
    function() {
        if ( xhrObj.readyState == 4 && 200 == xhrObj.status ) {
            eval(xhrObj.responseText);
        }
    };
xhrObj.open('GET', 'A.js', true); // must be same domain as main page
xhrObj.send('');

function getXHRObject() {
    var xhrObj = false;
    try {
        xhrObj = new XMLHttpRequest();
    }
    catch(e){
        var progid = ['MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0',
'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
        for ( var i=0; i < progid.length; ++i ) {
            try {
                xhrObj = new ActiveXObject(progid[i]);
            }
            catch(e) {
                continue;
            }
            break;
        }
    }
    finally {
        return xhrObj;
    }
}

XHR Injection

Like XHR Eval, the XHR Injection technique uses an XMLHttpRequest to retrieve the JavaScript. But instead of using eval, the JavaScript is executed by creating a script DOM element and injecting the XMLHttpRequest response into the script.
var xhrObj = getXHRObject(); // defined in the previous example
xhrObj.onreadystatechange =
    function() {
        if ( xhrObj.readyState == 4 ) {
            var scriptElem = document.createElement('script');
            document.getElementsByTagName('head')[0].appendChild(scriptElem);
            scriptElem.text = xhrObj.responseText;
        }
    };
xhrObj.open('GET', 'A.js', true); // must be same domain as main page
xhrObj.send('');
Reference: https://www.safaribooksonline.com/library/view/even-faster-web/9780596803773/ch04.html

Script in Iframe

Iframes are loaded in parallel with other components in the main page. Whereas iframes are typically used to include one HTML page within another, the Script in Iframe technique leverages them to load JavaScript without blocking.

Non-Render Blocking CSS

If you are going for completely non-render blocking CSS then you really only have one good option, and that is to inline your CSS.

Other references


  1. Render blocking javascripts
    https://varvy.com/pagespeed/render-blocking.html
  2. Perceived Web Performance – What is Blocking the DOM?
    https://www.keycdn.com/blog/blocking-the-dom/



No hay comentarios:

Publicar un comentario