6

Let's take the Google Analytics Universal script as premise of the practice I am looking to clarify, validate and/or expand on:

<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','//www.google-analytics.com/analytics.js','ga');
</script>

Google has chosen to pass all variables as arguments instead of declaring them with var. The clear benefit in this case, is to minify the code to a maximum (avoiding the var keyword in scope and the = character repeats) with a and m being undefined... While it is common to pass an existing variable as argument, or an object into scope, passing undefined variables as argument to skip declaring them is quite unusual.

I am interesting in knowing whether there are known caveats with that approach. Particularly:

Does this use more or less memory, or is it equivalent to declared vars in the function scope?

Does it make any difference in regard to the garbage collector, and how so?

And does an increased or extensive amount of passed arguments to a function have any downsides in terms of performance?

Additional Notes: Please, NO GUESS ANSWER. I am not interested in opinion based answers. Also the question is not about passing object scope as already answered at: https://stackoverflow.com/a/4665324/1647538. So please don't answer for that particular aspect.

I am only looking for verifiable fact-based answers from javascript seasoned experts or browser implementors, with extensive knowledge of javascript compilers, and how garbage collectors work.

Community
  • 1
  • 1
hexalys
  • 5,177
  • 3
  • 31
  • 56
  • 1
    I don't think that the *secret answer* you're looking for really exists. It is just minification. - You can do a speed test on http://jsperf.com/ to figure out if there is any difference in performance. - Also never use this in your original source code. The negative impact to readability negates *any* possible performance gain. – vbence Dec 19 '13 at 09:56
  • @vbence It's not a secret answer. It's about Garbage collector and memory usage facts. I have no use for opinions on the merits. I just want raw facts. I am not interested in what others think I should do with it, or whether it should matter. That's beside the point my question. – hexalys Dec 19 '13 at 21:01
  • Garbage collector is pretty specific to your JS engine. What helps in one browser may degrade performance in other. - Besides, aberration from mainstream coding style is never good, even if they help in the short run, JS engines will be optimized to better deal with the practices used in 90% of webpages. - If you really want to get an answer specific to this question, you can always check out the source of most the popular engines. – vbence Dec 19 '13 at 23:46

1 Answers1

1

Not sure about this case specifically, but injecting objects such as window and document rather than directly referencing them from inside of a function allows you to pass mock objects during unit testing.

JPR
  • 869
  • 4
  • 7
  • Yes I know. I referred to that "object into scope". The vars in context here are 'string' variables and essentially pre-declaring `undefined` args as subtitute for declaring vars in local scope. But for whatever reasons, people are voting heavily to close this question, even when there are upvotes and interest in the answer. I really don't get why that is... – hexalys Dec 19 '13 at 04:25