jquery - Javascript compression and the object properties issue -


the main javascript compressors , minifiers don't deal object properties names. (google closure, yui...)

i noticed there's big difference in resulting size (gzipped , don't gzipped) depending on way or pattern decide choose our script.

as example, choosing prototype pattern our project generate bigger resulting files (uncompressed, compressed , gzipped).

here's little comparison 2 portions of code doing same:

compressed using google closure compiler.

the result quite evident looking @ resulting compressed code:


prototype pattern


var myblueprint=function(){this.name="demo";this.somefunction=function(){alert("some function")};this.someotherfunction=function(){alert("some other function")};this.showmyname=function(){alert(this.name)};this.somefunction();this.someotherfunction();this.showmyname()};new myblueprint;


without pattern


var myblueprint=function(){alert("some function");alert("some other function");alert("demo")};new myblueprint;



using object properties won't compressed. such as:

//function declarations this.somefunction = function(){ ... }  //objects var demo = {     isactive: 'aaaa'.     name: 'aaaa' } 

should thinking when creating our projects? wouldn't first time rather having object full of properties decide use normal variables fact of compress long property names being used multiple times.

property names re-nameable closure-compiler using advanced_optimizations. in addition, full power of closure-compiler, need tell compiler myblueprint constructor using @constructor annotation. also, call "prototype pattern" in fact using instance methods - not prototypes.

here's updated comparisons:

closure compiler can inline methods in many cases making output size differences negligible. if can use closure-compiler advanced_optimizations, should choose code pattern provides best maintainability , let compiler handle optimizations.

however of these comparisons meaningless. have actual gzip compression comparisons need large code base. small snippets of code not provide accurate representations.


Comments