Google Analytics protocol check versus hardcoded string

JavaScript performance comparison

Revision 3 of this test case created

Info

The snippets I’m comparing here aren’t quite the same, but for HTTP-only websites it doesn’t make a difference. See the “Use the protocol check only when it’s needed” section in my article on optimizing the asynchronous Google Analytics snippet for more information.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var location = window.location;
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Ternary protocol check
var url = ('https:' == location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
pending…
ternary best
var url = ('https:' == location.protocol ? 'https://ssl.google-analytics.com/ga.js' : 'http://www.google-analytics.com/ga.js');
pending…
hardcoded string
var url = '//www.google-analytics.com/ga.js';
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment