Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 349
Silver Light Avatar asked Apr 24 '13 15:04

Silver Light


People also ask

Should I minify CSS and JS?

Should I Minify CSS and JS. Generally, the CSS and JS files of dynamic websites don't change very often so they are considered static files. Therefore, you can minify those CSS and JS files once and keep reusing those minified files.

Why do we minify JavaScript?

Minification is the process of minimizing code and markup in your web pages and script files. It's one of the main methods used to reduce load times and bandwidth usage on websites. Minification dramatically improves site speed and accessibility, directly translating into a better user experience.

Why should you minify your JavaScript scripts before publishing them?

These characters include whitespaces, line breaks, comments, and block delimiters which are useful for us humans but unnecessary for machines. We minify the files of a website containing CSS, HTML, and Javascript code so your web browser can read them faster.

How do I gzip CSS and JavaScript files?

htaccess file. Then load an html or js file via the server and check the headers for "Content-Encoding", if it says gzip or deflate, it is enabled. You need access to your vhost/server config to globally enable compression. You don't need to prepare your files, they're compressed automatically on request.


2 Answers

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.

like image 106
Pete Avatar answered Nov 10 '22 00:11

Pete


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

like image 45
Kenneth Avatar answered Nov 09 '22 23:11

Kenneth