javascript:哪个是强大的javascript压缩器

发布于 2024-10-15 21:35:06 字数 1432 浏览 5 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

横笛休吹塞上声 2024-10-22 21:35:06

我会推荐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,但它要求您根据他们的无警告编译指南来优化代码。

总结一下最佳实践

  1. 使用基于非评估的压缩器,例如 YUI。
  2. 为您的脚本提供 gzip
  3. 压缩 将所有脚本和依赖项压缩到一个文件中(不是在开发期间)或依赖项注入(大型应用程序),例如 Aaron Newtons Dependency Loader 或 require.js

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:

  1. Use a non-eval based compressor like YUI.
  2. Serve your scripts gzipped
  3. 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

深海蓝天 2024-10-22 21:35:06

除了生成的脚本的原始大小之外,您必须记住的另一件事是执行该脚本所需的性能。如果压缩的方式要求浏览器首先解压缩或重新生成原始代码,那么您就会造成性能损失,因此也许选择其他压缩器中的任何一个都会是更好的选择,特别是因为文件发送一次并缓存在浏览器中,但每次加载页面时都必须进行解压缩。

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.

熊抱啵儿 2024-10-22 21:35:06

正如卡萨布兰卡在他的评论中提到的,它们都会产生类似的结果,所以你应该更喜欢你最舒服的那个。

选择压缩库时需要考虑的另一件事是它如何与您的构建/发布过程集成。

另外,请考虑大多数 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文