在压缩之前缩小JavaScript有什么好处? [英] Is there a benefit to minifying JavaScript before gzipping it?

查看:102
本文介绍了在压缩之前缩小JavaScript有什么好处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在压缩之前是否有一些有效的目的来缩小?如果gzip文件首先缩小,那么gzip文件似乎不太可能。

Is there some valid purpose to minifying before compressing? It seems highly unlikely that the gzipped file is smaller if it's minified first.

我问,因为在缩小代码中诊断生产问题要困难得多,而且我想知道人们是否为了这个目的而无缘无故。

I ask because diagnosing production problems in minified code is substantially more difficult, and I'm wondering if people are subjecting themselves to that for no purpose.

推荐答案

是的,肯定有一个好处。

Yes, there is definately a benefit.

缩小是有损压缩而gzipping是无损的。 Ergo,缩小你删除不需要的数据(如注释和长变量名),这将总是有助于使您的文件更小。即使使用gzip,在大多数情况下仍然存在差异。

Minifying is a lossy compression whereas gzipping is lossless. Ergo, with minifying you remove unneeded data (like comments and long variablenames) which will always help to make your file smaller. Even with gzip there will still be a difference in most cases.

示例:

function foo(this_is_my_variable){
    var this_is_my_other_variable = 0;
    this_is_my_other_variable = this_is_my_other_variable + this_is_my_variable;
    return this_is_my_other_variable;
}

这可能会缩小为:

function foo(a){
    var b = 0;
    b = b +a;
    return b;
}

或者如果minifier是聪明的:

Or if the minifier is smart:

function foo(a){
    return a;
}

所有代码都给出相同的结果,但大小差异很大。

All code gives the same results, but the size differs a lot.

这篇关于在压缩之前缩小JavaScript有什么好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆