What is JavaScript Minification?
JavaScript Minification is the process of reducing the size of JavaScript files. To reduce the size, the comments, extra white spaces, and new line characters are removed from the script and the variable names are replaced with shorter variable names. Minification process does not affect the functionalities of JavaScript code.
How to Minify JavaScript?
There are several online tools available to minify the JavaScript. Just search the term ‘Minify JavaScript’ on the internet. You can use any of them. Let us check this by an example. In this example. I am using the jcompress.com tool.
Below is my JavaScript function,
- function AddNumbers(number1, number2)
- {
- var addition = number1 + number2;
-
- alert('Addition is:' + addition)
- }
In this code there is one blank line and one comment. See what happens after minifying this code.
- function AddNumbers(d,i){var n=d+i;alert("Addition is:"+n)}
The variables number1, number2, and addition are replaced with shorter names d, i and n. All white spaces, new lines and comments are removed from code.
Naming Convention for Minified JavaScript file
To differentiate minified files from original script files the word ‘min’ is used in the file name, for example there are two files for JQuery; one is ‘jquery.js’ and another is ‘jquery.min.js’. The second one is the minified version.
Advantages of Minification
- As minification reduces the size of JS file it reduces the download time and the web page loads much faster.
- It reduces the bandwidth consumption of the site.
- It improves the script execution time also.
- It reduces the number of HTTP requests to the server when combining many JavaScript files into one minified file, thus reducing the load on server and allowing more visitors to access web site.
Disadvantage of Minification
As white spaces and comments are removed, it is difficult to read and debug the JavaScript file.
So, in the development stage use non-minified JavaScript files. Just before deployment of application minify these files and deploy.
Conclusion
In this article we learned how minification helps to reduce the size of the JavaScript file and increases the performance of web applications.
Read more articles on JavaScript
Rajeev PunhaniPosted Mar 24, 2016, 5:42 AM
Nice share.
Pradeep SahooPosted Mar 18, 2016, 8:36 PM
which tools is used to minify , what is the best tool for minification
Ranjit PowarPosted Mar 17, 2016, 2:06 AM
Thanks thrish, sailesh
Saillesh PawarPosted Mar 17, 2016, 12:26 AM
Nice share
ThrishPosted Mar 16, 2016, 6:50 PM
Nice one.Thanks for sharing
Ranjit PowarPosted Mar 9, 2016, 10:51 PM
Thanks
Kumaresh RajalingamPosted Mar 9, 2016, 8:39 PM
Nice share
Ranjit PowarPosted Mar 9, 2016, 1:16 AM
Thanks sailesh
Shailesh UkePosted Mar 9, 2016, 1:04 AM
Nice Article..
Ranjit PowarPosted Mar 9, 2016, 12:11 AM
Thank you sir
Debasis SahaPosted Mar 9, 2016, 12:10 AM
Nice one...
Ranjit PowarPosted Mar 8, 2016, 10:58 PM
Thanks ranjan
Ranjan DailataPosted Mar 8, 2016, 12:13 PM
Nice article. I would recommend developers to use minifier's like JSMin -http://www.crockford.com/javascript/jsmin.html. Please note - There is a difference between minification and obfuscation.
Ranjit PowarPosted Mar 8, 2016, 3:56 AM
Thanks vignesh
Vignesh ManiPosted Mar 8, 2016, 3:40 AM
Nice
Ranjit PowarPosted Mar 8, 2016, 1:57 AM
Thanks shailesh.
Saillesh PawarPosted Mar 8, 2016, 1:41 AM
Nice Ranjit, adding how to minify your custom javascript file would be treat to the reader. Though i know how to do it but as a fresher i spend lot time finding out how to minify and apply jquery in custom js file.
Ranjit PowarPosted Mar 8, 2016, 1:22 AM
thanks
Ankur MistryPosted Mar 8, 2016, 1:13 AM
Nice