最好的 JavaScript 压缩器

发布于 2024-07-04 14:02:15 字数 111 浏览 7 评论 0 原文

最好的 JavaScript 压缩器是什么? 我正在寻找一种工具:

  • 易于使用
  • 具有高压缩率
  • 产生可靠的最终结果(不会弄乱代码)

What is the the best JavaScript compressor available? I'm looking for a tool that:

  • is easy to use
  • has a high compression rate
  • Produce reliable end results (doesn't mess up the code)

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

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

发布评论

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

评论(13

木槿暧夏七纪年 2024-07-11 14:02:15

Js Crush 是一个很好的压缩器,可以在缩小后使用。

Js Crush is a good compressor to use after you have minified.

狼性发作 2024-07-11 14:02:15

KJScompress

http://opensource.seznam.cz/KJScompress/index.html html

Kjscompress/csskompress 是两个一组
应用程序(kjscompress a
csscompress)删除不重要的
文件中的空格和注释
包含 JavaScript 和 CSS。 两个都
是命令行应用程序
GNU/Linux 操作系统。

KJScompress

http://opensource.seznam.cz/KJScompress/index.html

Kjscompress/csskompress is set of two
applications (kjscompress a
csscompress) to remove non-significant
whitespaces and comments from files
containing JavaScript and CSS. Both
are command-line applications for
GNU/Linux operating system.

七堇年 2024-07-11 14:02:15

bananascript.com 曾经给我最好的结果。

bananascript.com used to give me best results.

素罗衫 2024-07-11 14:02:15

这里是一个 YUI 压缩器脚本 (Byuic) 查找路径下的所有 js 和 css 并压缩/(可选)混淆他们。 很高兴集成到构建过程中。

Here is a YUI compressor script (Byuic) that finds all the js and css down a path and compresses /(optionally) obfuscates them. Nice to integrate into a build process.

泪痕残 2024-07-11 14:02:15

JSMin 是另一个。

JSMin is another one.

缘字诀 2024-07-11 14:02:15

在寻找灵丹妙药时,发现了这个
问题。 对于 Ruby on Rails
http://github.com/sstephenson/sprockets

In searching silver bullet, found this
question. For Ruby on Rails
http://github.com/sstephenson/sprockets

哥,最终变帅啦 2024-07-11 14:02:15

这是 HttpHandler 的源代码,它也许可以做到这一点它会帮助你

Here's the source code of an HttpHandler which does that, maybe it'll help you

許願樹丅啲祈禱 2024-07-11 14:02:15

我最近发布了 UglifyJS,一个用 JavaScript 编写的 JavaScript 压缩器(运行在 NodeJS Node.js 平台,但它可以轻松修改为在任何 JavaScript 引擎上运行,因为它不需要任何 <代码>Node.js内部)。 它比 YUI CompressorGoogle Closure,它的压缩效果比 YUI 在我测试过的所有脚本上,它比 Closure 更安全(知道处理“eval”或“with”)。

除了删除空格之外,UglifyJS 还执行以下操作:

  • 更改局部变量名称(通常为单个字符)
  • 连接连续的 var 声明
  • 避免插入任何不需要的括号、括号和分号
  • 优化 IF(当检测到不需要时删除“else”,转换如果可能的话,将 IF 插入 &&、|| 或 ?/: 运算符等)。
  • 在可能的情况下将 foo["bar"] 转换为 foo.bar
  • 从对象字面量中的键中删除引号,在可能的情况下
  • 解析简单表达式,因为这会导致更小的代码(1+3 *4 ==> 13)

PS:哦,它还可以“美化”。 ;-)

I recently released UglifyJS, a JavaScript compressor which is written in JavaScript (runs on the NodeJS Node.js platform, but it can be easily modified to run on any JavaScript engine, since it doesn't need any Node.js internals). It's a lot faster than both YUI Compressor and Google Closure, it compresses better than YUI on all scripts I tested it on, and it's safer than Closure (knows to deal with "eval" or "with").

Other than whitespace removal, UglifyJS also does the following:

  • changes local variable names (usually to single characters)
  • joins consecutive var declarations
  • avoids inserting any unneeded brackets, parens and semicolons
  • optimizes IFs (removes "else" when it detects that it's not needed, transforms IFs into the &&, || or ?/: operators when possible, etc.).
  • transforms foo["bar"] into foo.bar where possible
  • removes quotes from keys in object literals, where possible
  • resolves simple expressions when this leads to smaller code (1+3*4 ==> 13)

PS: Oh, it can "beautify" as well. ;-)

木落 2024-07-11 14:02:15

尝试 JSMin,获得了 C#、Java、C 和其他端口,并且也随时可用。

Try JSMin, got C#, Java, C and other ports and readily available too.

む无字情书 2024-07-11 14:02:15

YUI 压缩机 就是最佳选择。 它具有很高的压缩率,经过充分测试,并在许多顶级网站中使用,并且是我个人推荐的。

我已经将它用于我的项目,没有出现任何 JavaScript 错误或问题。 而且它有很好的文档。

我从未使用过它的 CSS 压缩功能,但它们也存在。 CSS 压缩也同样有效。

注意:虽然 Dean Edwards 的 /packer/ 实现了比 YUI Compressor 更好的压缩率,但我运行使用它时遇到一些 JavaScript 错误。

YUI Compressor is the way to go. It has a great compression rate, is well tested and is in use among many top sites, and, well, personally recommended by me.

I've used it for my projects without a single JavaScript error or hiccup. And it has nice documentation.

I've never used its CSS compression capabilities, but they exist as well. CSS compression works just as well.

Note: Although Dean Edwards's /packer/ achieves a better compression rate than YUI Compressor, I ran into a few JavaScript errors when using it.

遮云壑 2024-07-11 14:02:15

我使用 Dojo 项目中的 ShrinkSafe - 它很特殊,因为它实际上使用了JavaScript 解释器(Rhino)用于处理在代码中查找符号并了解其范围等。有助于确保代码在从另一端出来时能够正常工作,而不是许多使用正则表达式执行相同操作的压缩工具(这不那么可靠)。

我实际上有一个 MSBuild 任务

编辑:顺便说一句,“最佳”是有争议的,因为“最佳”的标准会根据项目的需求而变化。 就我个人而言,我认为 ShrinkSafe 是一个很好的平衡; 对于一些认为最小尺寸==最好的人来说,这是不够的。

编辑:值得注意的是,YUI 压缩器也使用 Rhino。

I use ShrinkSafe from the Dojo project - it is exceptional because it actually uses a JavaScript interpreter (Rhino) to deal with finding symbols in the code and understanding their scope, etc. which helps to ensure that the code will work when it comes out the other end, as opposed to a lot of compression tools which use regex to do the same (which is not as reliable).

I actually have an MSBuild task in a Web Deployment Project in my current Visual Studio solution that runs a script which in turn runs all of the solution's JS files through ShrinkSafe before we deploy and it works quite well.

EDIT: By the way, "best" is open to debate, since the criteria for "best" will vary depending on the needs of the project. Personally, I think ShrinkSafe is a good balance; for some people that think smallest size == best, it will be insufficient.

EDIT: It is worth noting that the YUI compressor also uses Rhino.

就是爱搞怪 2024-07-11 14:02:15

如果您使用 Packer,只需使用“收缩变量”选项并对生成的代码进行 gzip 压缩即可。 base62 选项仅适用于您的服务器无法发送 gzip 压缩文件的情况。 具有“shrink vars”的 Packer 可以更好地压缩 YUI,但如果您在某处跳过了分号,则可能会引入错误。

base62 基本上是一个穷人的 gzip,这就是为什么 gzip 压缩 base62 代码比 gzip 压缩收缩 var 代码提供更大的文件。

If you use Packer, just go far the 'shrink variables' option and gzip the resulting code. The base62 option is only for if your server cannot send gzipped files. Packer with 'shrink vars' achieves better compression the YUI, but can introduce bugs if you've skipped a semicolon somewhere.

base62 is basically a poor man's gzip, which is why gzipping base62-ed code gives you bigger files than gzipping shrink-var-ed code.

残月升风 2024-07-11 14:02:15

几年后重新审视这个问题, UglifyJS 似乎是目前最好的选择。

如下所述,它在 NodeJS 平台上运行,但可以轻松修改以在任何 JavaScript 引擎上运行。

--- 下面的旧答案 ---

Google 发布了 Closure Compiler到目前为止,这似乎生成了最小的文件 此处此处

在此之前的各种选项如下

基本上Packer 在初始压缩方面做得更好,但是如果您要在在线发送之前对文件进行 gzip(您应该这样做)YUI Compressor 获得最小的最终大小。

顺便说一句,测试是在 jQuery 代码上完成的。

  • 原始 jQuery 库 62,885 字节,gzip 后 19,758 字节
  • jQuery 使用 JSMin 缩小 36,391 字节,gzip 后 11,541 字节
  • jQuery 使用 Packer 缩小 21,557 字节,gzip 后 11,119 字节
  • jQuery 使用 YUI 压缩器缩小 31,822 字节,gzip 后 10,818 字节

@daniel james 在评论中提及 compressorrater 显示 Packer 在最佳压缩方面领先图表,所以我猜 ymmv

Revisiting this question a few years later, UglifyJS, seems to be the best option as of now.

As stated below, it runs on the NodeJS platform, but can be easily modified to run on any JavaScript engine.

--- Old answer below---

Google released Closure Compiler which seems to be generating the smallest files so far as seen here and here

Previous to that the various options were as follow

Basically Packer does a better job at initial compression , but if you are going to gzip the files before sending on the wire (which you should be doing) YUI Compressor gets the smallest final size.

The tests were done on jQuery code btw.

  • Original jQuery library 62,885 bytes , 19,758 bytes after gzip
  • jQuery minified with JSMin 36,391 bytes , 11,541 bytes after gzip
  • jQuery minified with Packer 21,557 bytes , 11,119 bytes after gzip
  • jQuery minified with the YUI Compressor 31,822 bytes , 10,818 bytes after gzip

@daniel james mentions in the comment compressorrater which shows Packer leading the chart in best compression, so I guess ymmv

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