protocol-relative URL (v2)

Revision 2 of this benchmark created on


Description

The idea of browsers supporting protocol relative URLs (e.g. <img src="//domain.com/img/logo.png">) is intriguing for linking to an external assets like JS and images when a site could be optionally loaded in an SSL or clear-text session. I wanted to test a wide variety of browsers for support of this method and any impact on load times this might have in comparison to traditional methods of client-side solutions.

Traditional methods of solving this problem on the client side require all assets to be on the same server (e.g. <img src="/img/logo.png">) or require a modification of the protocol with Javascript via an if/then or ternary test.

Google Analytics handles this via a JS ternary test:

ga.<span class="me1">src</span> <span class="sy0">=</span> <span class="br0">(</span><span class="st0">'https:'</span> <span class="sy0">==</span> document.<span class="me1">location</span>.<span class="me1">protocol</span> <span class="sy0">?</span> <span class="st0">'https://ssl'</span> <span class="sy0">:</span> <span class="st0">'http://www'</span><span class="br0">)</span> <span class="sy0">+</span> <span class="st0">'.google-analytics.com/ga.js'</span>

or as I have in the past:

document.<span class="kw1">write</span><span class="br0">(</span><span class="st0">'<span class="es0">\x</span>3Cscript src="'</span><span class="sy0">+</span>document.<span class="me1">location</span>.<span class="me1">protocol</span><span class="sy0">+</span><span class="st0">'//www.example.com/somefile.js"<span class="es0">\x</span>3E<span class="es0">\x</span>3C/script<span class="es0">\x</span>3E'</span><span class="br0">)</span><span class="sy0">;</span><span class="br0">}</span>

This topic has been discussed at length by Paul Irish: http://paulirish.com/2010/the-protocol-relative-url/

and the creator of jsperf, Mathias Bynens: http://mathiasbynens.be/notes/async-analytics-snippet


I am going to test loading a JS & IMG files via various methods and test for their success.

Test runner

Ready to run.

Testing in
TestOps/sec
Direct Link Control
var foo=bar;
ready
Ternary Test
var foo=bar;
ready
Protocol Relative Test
test 3
ready

Revisions

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