Asynchronous Google Analytics snippets (v24)

Revision 24 of this benchmark created by Aaron on


Description

Test case which measures the speed difference between the default asynchronous Google Analytics snippet and the optimized version.

Note that I commented out the s.parentNode.insertBefore(g, s); line in both scripts, since otherwise the <script> element gets inserted a few thousand times every time you run the tests. Since this code is the same in both scripts, it shouldn’t have an influence on the results.

Test runner

Ready to run.

Testing in
TestOps/sec
Default snippet
  (function(window, document, variableName, scriptElement, firstScript) {
    window['GoogleAnalyticsObject'] = variableName;
    window[variableName] || (window[variableName] = function() {
      (window[variableName].q = window[variableName].q || []).push(arguments);
    });
    window[variableName].l = +new Date;
    scriptElement = document.createElement('script'),
    firstScript = document.scripts[0];
    scriptElement.src = '//www.google-analytics.com/analytics.js';
    firstScript.parentNode.insertBefore(scriptElement, firstScript)
  }(window, document, 'ga'));
ready
Optimized snippet
    (function(b,l,i,p){b.GoogleAnalyticsObject=i;b[i]||(b[i]=function(){(b[i].q=b[i].q||[]).push(arguments)});b[i].l=+new Date;
    l.getElementsByTagName(p)[0].parentNode.appendChild(l.createElement(p)).src='//google-analytics.com/analytics.js'}
    (window,document,'ga','script'));
ready
no www
  (function(window, document, variableName, scriptElement, firstScript) {
    window['GoogleAnalyticsObject'] = variableName;
    window[variableName] || (window[variableName] = function() {
      (window[variableName].q = window[variableName].q || []).push(arguments);
    });
    window[variableName].l = +new Date;
    scriptElement = document.createElement('script'),
    firstScript = document.scripts[0];
    scriptElement.src = '//google-analytics.com/analytics.js';
    firstScript.parentNode.insertBefore(scriptElement, firstScript)
  }(window, document, 'ga'));
ready
ga
    (function(i, s, o, g, r, a, m) {
        i['GoogleAnalyticsObject'] = r;
        i[r] = i[r] || function() {
            (i[r].q = i[r].q || []).push(arguments)
        }, i[r].l = 1 * new Date();
        a = s.createElement(o), m = s.getElementsByTagName(o)[0];
        a.async = 1;
        a.src = g;
        m.parentNode.insertBefore(a, m)
    })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.