What is Javascript minification?
Minification in Javascript is the process of removing all characters that are not necessary from the Javascript source code. That is why it is called “minification” – because all of the data that is not necessary to the functioning of the Javascript is removed from the source code, and therefore the Javascript is “minimized”. Even though these characters are removed from the Javascript source code, the functionality of the Javascript code does not change at all. So, your Javascript code will behave exactly the same even after it goes through the minification process. Code that has gone through the minification process is also known as “minified” code.
What data is removed in Javascript minification?
The data that is removed in Javascript minification is extra whitespace, comments, new line characters, etc. Basically anything that can be removed without compromising the functionality of the Javascript is removed.
How are Javascript files minified?
There are a lot of programs that will minify your Javascript code for free – all you have to do is simply google “Javascript minifier”, and these sites will simply ask you to submit your Javascript code and will return the minified result.
What are the benefits and advantages of Javascript minification?
The main purpose of Javascript minification is to speed up the downloading or transfer of the Javascript code from the server hosting the website’s Javascript. The reason that minification makes downloads go faster is because it reduces the amount of data (in the minified Javascript file) that needs to be downloaded. Less data means that the user’s browser spends less time processing that data, which is why time is saved. So, we can say that minification is performed on Javascript source code because it is essentially a performance enhancement – and it allows websites that use minified Javascript to load faster.
To summarize, some of the benefits of Javascript minification are:
- If you are a website owner, your users will have quicker download times for your webpages because there is now less data in your Javascript files that needs to be downloaded.
- Also, if you are a website owner, your site will have less bandwidth consumption. This means that your servers will consume less energy and you will save money. If your site just has a few visitors everyday, the bandwidth savings will be meaningless. But, think of a large site like Facebook, which gets hundreds of millions of users each day, and how Javascript minification could save a lot of bandwidth for them.
Minification is NOT like normal data compression
Normally when you think of data compression, one of the first things that may come to mind is “zipping” a file. When you zip a file, you are compressing it using a special algorithm, and when someone else has to unzip that file, they have to decompress the file using a special program (which you may have used without knowing it since it comes with most operating systems built in). However, with Javascript minification, no special program is needed by the browser in order to understand how to read the minified Javascript – because minified Javascript is still readable by the browser as is. A human can even read it – although with great difficulty since there won’t be any whitespace and newline characters. But, the point is that no intermediate program is needed in order to convert the minified Javascript into a form that is readable by the browser, and for that reason it is very different from normal data compression.
Minification does apply to other programming languages as well
Even though we discuss minification within the context of Javascript in this article, it does apply to other languages as well. For example, both CSS and HTML can be and often are minified for the same reasons as Javascript.
Obfuscation in Javascript
You may have also heard of the term obfuscation used in conjunction with the term minification in Javascript. You can read an easy to understand article about the difference between obfuscation and minification here: Minfication vs Obfuscation.