Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

Generate more compessor-friendly javascript #26

Open
dimzon opened this issue Jul 11, 2011 · 3 comments
Open

Generate more compessor-friendly javascript #26

dimzon opened this issue Jul 11, 2011 · 3 comments

Comments

@dimzon
Copy link

dimzon commented Jul 11, 2011

Wrap class definitions into anonimous functions and use variables instead fully qualified

(function(){
...
  var _$$System$Drawing$Bitmap$prototype = System.Drawing.Bitmap.prototype;
  _$$System$Drawing$Bitmap$prototype._ctor$1 = function (filename, b) {
    _$$System$Drawing$Bitmap$prototype._ctor$0.call(this, filename);
  };
...
})();
@kg
Copy link
Member

kg commented Jul 11, 2011

How does that help compressors specifically? It looks like the resulting JS would be larger and more complicated, so I don't know how that would help the compressor. Is there a specific compressor that struggles with the way the code works now so I can take a look?

@dimzon
Copy link
Author

dimzon commented Jul 11, 2011

Google Closure Compiler, Yahoo Compressor and other good compressors can rename local variables and parameters (_$$System$Drawing$Bitmap$prototype) to something one-letter:

(function(){
 ...
 var a = System.Drawing.Bitmap.prototype;
 a._ctor$1 = function (x, b) {
 a._ctor$0.call(this, x);
 };
 ...
 })();

So You can use long significant names for local variables (to maintain readability) BUT compiler will optimize it reducing javascript size a lot.

the best way is to make JSIL.MakeClass to return a function itselt so you can generate something like this

//Trick: using unassigned parameter instead local variable definition (,v instead var v will save some chars)
(function(_$$System$Drawing$Bitmap,_$$System$Drawing$Bitmap$prototype){
_$$System$Drawing$Bitmap$prototype=_$$System$Drawing$Bitmap.prototype;
// Class definition below
 _$$System$Drawing$Bitmap$prototype._ctor$1 = function (filename, b) {
 _$$System$Drawing$Bitmap$prototype._ctor$0.call(this, filename);
 };
})(JSIL.MakeClass(.....));

@dimzon
Copy link
Author

dimzon commented Jul 11, 2011

other trick is to use object['field'] instead of object.field notation
look at this sample

  1. unoptimized original code
obj.prototype.foo=....;
obj.prototype.bar=....;
obj.prototype.aaa=...;
  1. prepared code
var $prototype='prototype'; // must be LOCAL variable
obj[$prototype].foo=....;
obj[$prototype].bar=....;
obj[$prototype].aaa=...;
  1. prepared after compilation (LOCAL variable names will be shortened to 1 char)
var p='prototype'; // must be LOCAL variable 
obj[p].foo=....;
obj[p].bar=....;
obj[p].aaa=...;

this trick works great if you combine whole assembly definition into one anonimous function and apply this trick for wide-used "apply" "call" "prototype" etc

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants