Case1: There are two snippets of code that are very similar and very gzip-able, one at the start of the document and the other is beyond 32kb at the end of the document. Does the minifier bring the two snippets together so that gzip will reach the second snippet?
Case 2: A snippet of code is used multiple times. Gzip repeating it would take a byte. But if that snippet is put in a function, it would require 1 byte for function
, at least 1 byte for the function name, 1 byte for () {
, and one byte for }
. Does the minifier turn functions back into repeated snippets of code for gzip?
I’m sure there are more cases I haven’t thought of, but these two have been on my mind for some time and I’d like to either put them to rest or start implementing them in my own code.
These are both highly unlikely, and I would actively avoid any optimiser which did make these transformations because I would suspect that it might have subtle bugs.
Case1: There are two snippets of code that are very similar and very gzip-able, one at the start of the document and the other is beyond 32kb at the end of the document. Does the minifier bring the two snippets together so that gzip will reach the second snippet?
Moving the two snippets together means reordering code. That might be perfectly safe, but determining whether it is safe or not is a hard problem (I’m sure that with a bit of effort there’s an example which proves it undecidable).
Case 2: A snippet of code is used multiple times. Gzip repeating it would take a byte. But if that snippet is put in a function, it would require 1 byte for function, at least 1 byte for the function name, 1 byte for () {, and one byte for }. Does the minifier turn functions back into repeated snippets of code for gzip?
This would definitely be unsafe. The minifier has no way of knowing whether other JavaScript in a separate script accesses that function.
Yes, though, not necessarily in the manners you’ve described.
But, there’s at least 1 very specific case wherein Google’s Closure Compiler optimizes for gzip:
Closure Compiler can even tell when two different variables are never
used at the same time, letting both share the same name and ensuring
that as many variables as possible use very short names for better
gzip compression. (Google)
Minifiers reduce the amount of disk space taken up by a file while preserving it’s functionality. They typically do so by deleting technically unnecessary white-spaces, indents, new lines and comments. Then they’ll reduce variable names down to letters
function helloWorld() {
//helloWorld string would be preserved
console.log("helloWorld");
}
//Please don't ever write code like this
var HelloWorld = helloWorld();
Would minimize to
function a(){console.log("helloWorld");}var b=a();
This has nothing to do with gzip, which is a separate type of compression that will be done on top of your minified javascript files. Unlike a minifer, the file is compressed where it has to be uncompressed before it can be executed.
The benefits you receive from gzip will technically depend on the contents of the file but this isn’t something you need to worry about. gzip optimizes for you, not the other way around