I would recommend YUI Compressor. It is also used for to the official compressed version and pretty efficient. It is Java based and I use it during deployment but also on-demand (which needs caching since it is not very fast).
bananascript uses various String methods like split, replace to build the script and the worst you see in the end of the script: eval. This is the reason why even Dean Edwards packer is rarely used these days, eval adds a lot of computing overhead on each load and makes the browser window feel sluggish on each load. (More here: http://dean.edwards.name/weblog/2007/08/js-compression/)
You can give Closure Compiler from Google a try, but it requires you to optimize your code according to their guidelines for warning free compilation.
To summarize the best practices:
Use a non-eval based compressor like YUI.
Serve your scripts gzipped
Compress all your scripts and dependencies in one file (not during development) or dependency injection (large scale applications) like Aaron Newtons Dependency Loader or require.js
Besides the raw size of the generated script, another thing that you must keep in mind is the performance required to executed that script. If the compression is done in a way that it requires the browser to first decompress or regenerate the original code then you are creating a performance hit so maybe choosing anyone of the other compressors would have been a better choice, specially as the files are sent once and cached at the browser, but the decompression must be done everytime the page is loaded.
另外,请考虑大多数 Web 服务器可以配置为对最终结果文件应用 gzip(或类似的)压缩,从而使通过线路发送的实际位数更小。
As casablanca mentioned in his comment, they're all going to yield similar results, so you should prefer the one that you're most comfortable with.
Something else to consider when choosing a compression library is how will it might integrate with your build/release process.
Also, consider that most web servers can be configured to apply gzip (or similar) compression to the end-result file -- making the real number of bits sent across the wire even smaller.
发布评论
评论(3)
我会推荐YUI 压缩机。它也用于官方压缩版本并且非常高效。它基于 Java,我在部署期间使用它,但也按需使用它(需要缓存,因为它不是很快)。
下载地址:http://developer.yahoo.com/yui/compressor/
在这里测试一下: http://yui.2clics.net/
Bananascript 使用各种字符串方法,例如 split、replace to构建脚本以及您在脚本末尾看到的最糟糕的情况:eval。这就是为什么现在连 Dean Edwards packer 都很少使用的原因,eval 在每次加载时增加了大量的计算开销,并使浏览器窗口在每次加载时都感觉迟缓。 (更多信息请参见:http://dean.edwards.name/weblog/2007/ 08/js-compression/)
您可以尝试一下 Google 的 Closure Compiler,但它要求您根据他们的无警告编译指南来优化代码。
总结一下最佳实践:
I would recommend YUI Compressor. It is also used for to the official compressed version and pretty efficient. It is Java based and I use it during deployment but also on-demand (which needs caching since it is not very fast).
Download is here: http://developer.yahoo.com/yui/compressor/
Test it here: http://yui.2clics.net/
bananascript uses various String methods like split, replace to build the script and the worst you see in the end of the script: eval. This is the reason why even Dean Edwards packer is rarely used these days, eval adds a lot of computing overhead on each load and makes the browser window feel sluggish on each load. (More here: http://dean.edwards.name/weblog/2007/08/js-compression/)
You can give Closure Compiler from Google a try, but it requires you to optimize your code according to their guidelines for warning free compilation.
To summarize the best practices:
除了生成的脚本的原始大小之外,您必须记住的另一件事是执行该脚本所需的性能。如果压缩的方式要求浏览器首先解压缩或重新生成原始代码,那么您就会造成性能损失,因此也许选择其他压缩器中的任何一个都会是更好的选择,特别是因为文件发送一次并缓存在浏览器中,但每次加载页面时都必须进行解压缩。
Besides the raw size of the generated script, another thing that you must keep in mind is the performance required to executed that script. If the compression is done in a way that it requires the browser to first decompress or regenerate the original code then you are creating a performance hit so maybe choosing anyone of the other compressors would have been a better choice, specially as the files are sent once and cached at the browser, but the decompression must be done everytime the page is loaded.
正如卡萨布兰卡在他的评论中提到的,它们都会产生类似的结果,所以你应该更喜欢你最舒服的那个。
选择压缩库时需要考虑的另一件事是它如何与您的构建/发布过程集成。
另外,请考虑大多数 Web 服务器可以配置为对最终结果文件应用 gzip(或类似的)压缩,从而使通过线路发送的实际位数更小。
As casablanca mentioned in his comment, they're all going to yield similar results, so you should prefer the one that you're most comfortable with.
Something else to consider when choosing a compression library is how will it might integrate with your build/release process.
Also, consider that most web servers can be configured to apply gzip (or similar) compression to the end-result file -- making the real number of bits sent across the wire even smaller.