最好的 javascript 编译器/压缩器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5349423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:47:14  来源:igfitidea点击:

Best javascript compiler/minifier

javascriptcompiler-constructionminify

提问by gucki

I remember having seen a new javascript compiler/ minifier on github which should even be better than google's closure compiler. Unluckily I can't remember its name and find it again. Maybe someone can help me? :)

我记得在 github 上看到过一个新的 javascript 编译器/压缩器,它甚至应该比谷歌的闭包编译器更好。不幸的是我不记得它的名字并再次找到它。也许有人可以帮助我?:)

Thanks, Corin

谢谢,科林

采纳答案by Bryan Downing

Possibly UglifyJS? It's the minifier that the jQuery project is currently using.

可能是UglifyJS?它是 jQuery 项目当前正在使用的压缩器。

Check out these speed comparisons.

查看这些速度比较

回答by Samuel

See the statistics src: http://axod.blogspot.ca/2010/01/google-closure-compiler-advanced-mode.html

查看统计源代码:http: //axod.blogspot.ca/2010/01/google-closure-compiler-advanced-mode.html

compare JSMin, by Douglas Crockford and Yahoo! YUI

比较 JSMin,由 Douglas Crockford 和 Yahoo! 唯

Minifier           | Bytes   |% of original| gz Bytes | gz % original
None               | 428,264 |    100%     |   91,750 |     100%
JSMin              | 249,372 |     58%     |   57,338 |     62%
YUI                | 235,214 |     55%     |   55,990 |     61%
Closure (STANDARD) | 219,446 |     51%     |   53,515 |     58%
Closure (ADVANCED) | 166,774 |     39%     |   47,372 |     52%

Closure takes the idea of a Javascript minifier a step further. It does this by doing real compilation to remove unused code, inlines variables and rewrites code to make it as small as possible.

Closure 将 Javascript 缩小器的想法更进一步。它通过进行真正的编译以删除未使用的代码、内联变量并重写代码以使其尽可能小来做到这一点。

Just for reference, you can access the Google Closure Compiler UI here http://closure-compiler.appspot.com/home

仅供参考,您可以在此处访问 Google Closure Compiler UI http://closure-compiler.appspot.com/home

回答by John

AFAIK Closure Compiler's ADVANCED mode is the only game in town if you want global dead code removal (aka tree shaking) and optimization. Which is why projects like emscripten and ClojureScript, and Dart javascript translator use it.

如果您想要全局死代码删除(又名摇树)和优化,AFAIK Closure Compiler 的 ADVANCED 模式是镇上唯一的游戏。这就是 emscripten 和 ClojureScript 以及 Dart javascript 翻译器等项目使用它的原因。

回答by Robert Munn

This is an old question, but I came across some new information and wanted to share. I found a specialized minifier on GitHub called RegPack, https://github.com/Siorki/RegPack. I say specialized because it is intended to optimize small (1KB-4KB) files and works with Canvas, WebGL, and Audio components in JavaScript. The results are fairly astounding but also specific to the kind of JavaScript you are writing.

这是一个老问题,但我遇到了一些新信息并想分享。我在 GitHub 上找到了一个专门的压缩器,叫做 RegPack,https://github.com/Siorki/RegPack。我说专业是因为它旨在优化小 (1KB-4KB) 文件,并与 JavaScript 中的 Canvas、WebGL 和音频组件一起使用。结果相当惊人,但也特定于您正在编写的 JavaScript 类型。

Having noted that, I just ran Closure Compiler in standard mode against a library I wrote, https://github.com/robertdmunn/gadget-ui, to compare the output with UglifyJS, which I am using through Grunt. The base code is 20KB, Uglifier and Closure Compiler (std mode) both made it 10KB. For most use cases, I see that as good enough. Closure advanced mode only reduced it to 9KB. For edge cases - web scale - 10% smaller is decent. Consider, though, that gzip compression will reduce file size by 60-80% in my experience, so the 10% reduction using advanced mode only saves you 2-4% above standard mode if you use gzip. 2-4% is valuable at web scale, but for most developers, it might not be worth the potential problems introduced.

注意到这一点后,我只是针对我编写的库https://github.com/robertdmunn/gadget-ui以标准模式运行 Closure Compiler ,以将输出与我通过 Grunt 使用的 UglifyJS 进行比较。基本代码是 20KB,Uglifier 和 Closure Compiler(std 模式)都变成了 10KB。对于大多数用例,我认为这已经足够了。关闭高级模式仅将其减少到 9KB。对于边缘情况 - 网络规模 - 小 10% 是不错的。但是,请考虑一下,根据我的经验,gzip 压缩会将文件大小减少 60-80%,因此,如果使用 gzip,使用高级模式减少 10% 只会比标准模式节省 2-4%。2-4% 在 Web 规模上是有价值的,但对于大多数开发人员来说,可能不值得引入潜在的问题。