Why do people minify css and javascript, when we have gzip? [duplicate]

纵饮孤独 提交于 2019-11-30 08:11:32

问题


Why people go through all the pain of minifying JavaScript and CSS files if they can just turn on gzip compression on the web server? This will give the same (or even better) result in traffic preformance.

Does minification give additional benifits?


回答1:


You can use gzip (which is usually built into web servers) in combination with Minification. Minification does a lot of additional things that gzip can't, however, like removing comments, renaming long variables to shorter variable names, etc.

The resulting transferred data can be signicantly smaller than simply gzipping the original .js. It depends, obviously, on the source .js.

You can check out Compressorater(http://compressorrater.thruhere.net/). You can throw in your .js and it will minify using a variety of libraries with and without gzip and show you the results. You can see the comparion between simply gzipped and gzipped + minified by the various libraries.




回答2:


Minifying Javascript and CSS not only zips it but it adds other optimisations that are impossible by zipping.

For example, by minifying you can modify the name of a long variable. All the instances of that variable will then be only one character. Another thing minification does it removing comments. This cannot be done by gzip.

Apart from that minification usually bundles various files into one thereby reducing the amount of requests

Apart from minification you should ALSO use gzip



来源:https://stackoverflow.com/questions/16196358/why-do-people-minify-css-and-javascript-when-we-have-gzip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!