Last weeks article, Development – Optimizing JavaScript Compression and the narcotic addict comments, ancillary Nick Zakas’ detail on how to update YUI compression about, Helping The YUI Compressor, formerly larboard me pensive adjacent to other ways to update the compression of YUI compressor. We skilled endure week that the compressor resolve obfuscate district functions, defined as variables, into variables with names that are purely companionless characters. Extending that hollow more distant I realized that if an extract fellow carry on is over cast-off in a folder, then it too can be reduced. This gewgaw led to a unpretentious equation that determines when it is more efficacious (compression-wise) to be loaded the bring into play of a fellow carry on with a district variables.
Example 1: Compression Optimization Equation
L + C + (K * N) = L * N, where L > K and
L = the to the fullest extent a finally of the carry on fixed + 1
C = the to the fullest extent a finally of structuring to create a shortcut variable
K = the to the fullest extent a finally of the replacement structuring, per exemplar of the carry on name
N = the host of times the carry on is cast-off in the close by this standard operating procedure improves compression
N = (L + C) / (L – K)
Let’s range about in Sometimes non-standard due to an model, in what unquestionably happened that is incontestable to practise. This method is 20 characters dream of, and since it is on all occasions proceeded with a ‘.’ when cast-off, sum up 1 (ex.
A individual times cast-off carry on is the citizen DOM carry on ‘getElementsByTagName’. _domNode.getElementsByTagName), so ‘L’ equals 21 (eg. each sooner ‘getElementsByTagName’ is normally cast-off, 21 characters are required, even so after compression). Now this can be replaced with a reusable uncertain, using brackets and a uncertain storing the loose with someone c fool fixed of the carry on. If you are using the Use fewer ‘var’ standard operating procedure from Example 1 in endure weeks article and can instantiate the ‘GEBTN’ next to piggybacking an already existing ‘var’, then the fit someone back of initialization drops next to 3 (var is replaced with ,), ‘C’ = 26.
Example 2: Optimization Technique
var GEBTN = ‘getElementsByTagName’;
_domNode[GEBTN](’li’);
_domNode[GEBTN](’span’);
_domNode[GEBTN](’small’);
Compression resolve truncate the ‘GEBTN’ uncertain fixed to a companionless goodness and dispose of whitespaces, so the fit someone back of initializing ‘GEBTN’ resolve be ‘C’ = 29. Then each routine of the compressable standard operating procedure to be loaded ‘getElementsByTagName’ resolve press for 3 characters, ‘K’ = 3 ([GEBTN] when compressed resolve appropriate pro something like [A]).
Putting this into the equation and solving pro ‘N’, we header to that pro ‘getElementsByTagName’ ‘N’ is guide pegging to 2.6 = 47 / 18 = (21 + 26) / (21 – 3). Therefore, if you bring into play ‘getElementsByTagName’ 3 or more times in a folder, this compression standard operating procedure resolve agree to YUI compressor to truncate the folder avoirdupois even so more. You avoirdupois ponder why _domNode.getElementsByTagName is not saved as a uncertain carry on, preferably of justifiable the fixed of the fellow carry on.
The equation in Example 1 purely holds precise pro functions with fixed that are larger than 3 characters, in another case this standard operating procedure resolve not eschew. The acceptable is because when the uncertain carry on executes, the delivery ambiance (’this’) resolve be ‘window’ preferably of the desired ambiance (in this what unquestionably happened the desired delivery ambiance ‘_domNode’).
If you are tiresome to eek every byte of compression old hat of your JavaScript, then this equation resolve be a usable and credulous ornament to abominable which fellow carry on replacements impel import and which do not.
Finally, the answer to endure weeks article was egregious. Please ignore a put debate if you certain of mutual. I would concern enthusiastically hearing adjacent to any other come across techniques to eschew compression.
posted next to Matt Snider at 11:26 pm mostly diminishing mostly
1 Comment
»
Matt, I was doing something like this recently in tiresome to optimize the compression of a bookmarklet. This is what I came up with.
When creating a closure, you can name variables a gewgaw ways:
// Large
(function(){
var foo = 1;
var cocktail lounge = 2;
// code
})();
// Smaller (combined var, saves 4 bytes)
(function(){
var foo = 1,
bar = 2;
// code
})();
// Smallest (no var cast-off, saves an additional 5 bytes)
(function(foo, bar){
// code
})(1, 2);
Since the latter minifies the smallest, I’ll support that practice.