Google Closure munging efficiency

Go To


I have a JavaScript file that I ran through the Google Closure compiler, and it munged the name on to za, which is obviously not more efficient from a length standpoint. Is there some reason it is, in fact, more efficient (possibly because of the letters used), or is it just a couple random letters?

For instance, I can imagine that za might be more compressible than on when Gzipped, but there's a good chance it's just a couple of random letters.

2012-04-04 20:44
by exupero
on is actually likely to be more compressible than za because the Huffman encoding for 'z' might well be longer (more bits) than that of other letters due to it's relative infrequency in standard language - Eric J. 2012-04-04 20:57


After playing around with some, it seems that the compiler just start renaming the variable regardless of their previous name. It stays at 'a' and makes it way through 'b', etc... It does not appear to care about current length of the variable name, but it will make sure the overall size is the same or smaller than before. In some cases variable may be refactored out of the code if you use the advanced optimization setting.

By letting the compiler handle the naming of your variables it is sure that none of them conflict with each other. If it tried to keep your existing variable name it would have to keep track of that separately and maintain a list to compare against.

Another benefit of minifying code is to obfuscate your code base. Sure someone can figure it out if they really wanted to, but by making the variable names programatic it makes it harder to figure out the meaning.

If you really wanted to debug through combined & minified code I would recommend looking into the source maps feature of Google Chrome Canary

2012-04-04 21:17
by Elijah Manor
Yep, it picks names (from the pool of available names) based on size and frequency. The more frequent a name is used the shorter the name assigned to it is. Local variables have special handling but the basic idea is the same - John 2012-04-06 06:05


Is there some reason it is, in fact, more efficient (possibly because of the letters used)


or is it just a couple random letters?

Pretty much. AFAIK, Closure Compiler (and other JS minifiers) systematically renames variables, with the goal of using fewer characters overall, without looking at what they are already named.

2012-04-04 20:47
by Matt Ball