Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minify and variable names

I know that minification is responsible for removing: white space characters, new line characters, comments, and sometimes block delimiters. Not long ago I read it's also responsible for shorten variable names. But I always thought it's a part of obfuscation. Am I right? Or now minification libraries also include such functionality?

like image 282
Viacheslav Kondratiuk Avatar asked Oct 07 '12 12:10

Viacheslav Kondratiuk


People also ask

Does minify JavaScript change variable names?

The answer to your question in most cases of minification is: Yes, most minifiers will randomly rename variable/function/etc. names, starting with single letters, if needed extend to 2-letters, and continuing to avoid long names.

What is the point of minify?

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.

What is the difference between minify and uglify?

Minification is just removing unnecesary whitespace and redundant / optional tokens like curlys and semicolons, and can be reversed by using a linter. Uglification is the act of transforming the code into an "unreadable" form, that is, renaming variables/functions to hide the original intent...

Should you minify your JS?

It is important to minify your CSS and minimise JavaScript files so they can load faster on your web pages. There are many reasons why you should minify your CSS and JavaScript: Reduce file size: The more code there is in a file, the larger it will be. Minified code is usually much smaller than the original version.


1 Answers

Well, since the objective of minification is to reduce the size of the code as much as possible, renaming variables is an effective way of doing just that.

A trick that JavaScript minifiers often use, is to wrap the code in a immediately executed function, with a lot of arguments:

(function(a,b,c,d,e,f,g){/* ... */})();

This makes it possible to use these variables without declaring them with the var keyword, thus reducing the size of your code by three bytes times the number of var keywords.

Modern minifiers use a lot of these advanced tricks to reduce the size of your code, that the code seems obfuscated is just a bi-product of the minification.

like image 178
Saebekassebil Avatar answered Sep 18 '22 13:09

Saebekassebil