我注意到缩小器不会用原型javascript执行那么好,因为如果它们以此开始,它们不会替换任何方法或属性。例如:JavaScript优化和缩小与gzipping
// unoptimized 182 bytes
myClass.prototype.myFunction = function(){
this.myElementDom.style.backgroundColor='#000';
this.myElementDom.style.color='#FFF';
this.myElementDom.style.borderColor='#DDD';
}
// 168 bytes = 92% of unoptimized, YUI compressed
myClass.prototype.myFunction=function(){this.myElementDom.style.backgroundColor="#000";this.myElementDom.style.color="#FFF";this.myElementDom.style.borderColor="#DDD"};
// optimized 214 bytes
// set a replaceable local scope variable and reduce 2 variable
// lookups at the same time
// file-size in the development version doesn't matter, so we can even increase it
// to preserve readability
myClass.prototype.myFunction = function(){
var myElementDomStyle = this.myElementDom.style
myElementDomStyle.backgroundColor='#000';
myElementDomStyle.color='#FFF';
myElementDomStyle.borderColor='#DDD';
}
// 132 bytes = 72.53% of unoptimized, YUI compressed
myClass.prototype.myFunction=function(){var a=this.myElementDom.style;a.backgroundColor="#000";a.color="#FFF";a.borderColor="#DDD"};
华友世纪,19.47%保存...不要...发布用gzip脚本启用,未优化,YUI压缩版本加载与130个字节(= 71.42%来自未优化),显然在收益比优化后的YUI压缩版本节省了134个字节(=未优化的73.63%)......在考虑压缩是如何工作的时候可能是显而易见的,但现在要走了吗?首先做这种微型优化和较小的压缩,用gzip证明更大的文件大小......因为通过这种优化,您可以轻松地使您的代码更少可读性和可维护性。
您可以使用google closure minifier。 'gzip' +'minify'仍然比只有'gzip'好。 – kirilloid 2012-04-15 23:15:45
不是第二个例子更多关于在运行时减少DOM查找,而不是寻找空间节省? – 2012-04-16 11:22:22