Most of you are probably familiar with this little tracking code offered by Google Analytics.
<script>
(
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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-00000000-0', 'auto');
ga('send', 'pageview');
</script>
The interesting part is that this little excerpt contains arguments that form the word isogram. This script also uses arguments to declare variables to shave off some bits from the final file size. Arguably, you would not use this pattern when writing the code (?), so my question is, how does Google minify its code and are these techniques available to mere mortals, too?
I found online this example by Stephen Morley that includes a code that looks like something you'd write before minifying it. I took that code and run it through Google's very own Closure Compiler on advanced optimisation. As expected, the resulting code is nothing like the actual script used by Google Analytics.
(function(){window.b="ga";"ga"in window||(window.a=function(){window.a.q.push(arguments)},window.a.q=[]);window.a.c=(new Date).getTime();var c=document.createElement("script");c.src="//www.google-analytics.com/analytics.js";c.async=!0;var d=document.getElementsByTagName("script")[0];d.parentNode.insertBefore(c,d)})();
This time, the code is less DRY and bigger, even without the two extra commands.
So to clarify, I am curious how the Google engineers arrived to the above result (I do not think their code actually looks like the one in Stephen's example), and could this process be replicated even if you are not a part of Google? Thank you in advance!
I have a feeling that the word "isogram" is a bit of a sly hint from the Google employee who minified this code.
Since an isogram is a word with no repeating characters, it represents the precise logic necessary for the minification of parameters and other variable names that must be unique from one another.
In all likelihood, this term was baked into the minifier, so that the first collection of minified variables would indicate that they know a bit about the logic of unique letter sequencing.
Since the word isogram
is itself an isogram, the person who created the minification logic could set it to check a parameter or argument list for a case in which there are 7 arguments/params, and in that case simply replace each with the corresponding letter in the word "isogram". This would add some overhead, but such cases are rare, and Google does have a lot of servers and network engineers to optimize their scripts.