Google Analytics protocol check versus hardcoded string

JavaScript performance comparison

Test case created by Mathias Bynens and last updated

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
('https:' == location.protocol ? '//ssl' : '//www') + '.google-analytics.com/ga.js';
pending…
Hardcoded string
'http://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:

2 comments

Abdelkrim commented :

result quite astonishing!

John-David Dalton commented :

Millions of ops/sec vs. millions of ops/sec for something that is executed 1 time means there is no real world perf concern here. Also the test with a simple string literal might be being optimized away as dead code.

Add a comment