javascript compression techniques and tips

2

I recently found another way to use true and false replacing them with ! and ! . I was thinking of other ways to compress the codes or make them more "secret" but I did not find anything on the internet talking about it, just giving links to javascript compressors.

I even tried to look at JS bookstores, but you know, do not understand anything, so I wanted to know what 'techniques' or tips you use to reduce the size of the codes.     

asked by anonymous 12.06.2014 / 08:21

1 answer

7

Do not "compile your code by hand"! Remember that "programs should be written for people to read, and just by chance for machines to run."

The reason people use minifiers (or obfuscators) of JavaScript is precisely because this manual work is unnecessary, repetitive, and error prone:

  • Your code gets harder to read;
  • Your code gets harder to debug (when you see an error message, it's harder to find it in the sources);
  • Simple errors (such as exemplified by Sergio in comments ), which would be immediately visible in a "clean" code could go unnoticed in a code that uses many tricks of this type.
  • Often the process of minimizing code is "dumb" (eg, changing all variables from many letters to single letter variables), so it is better to delegate this task to a tool than to do it yourself.
    • And by complementing, this tool might do things you would not think of doing in the original source code (eg remove all line breaks and indentations).
  • And, in the end, most webservers compress content (using gzip or deflate) before sending to clients, so even gains from minification do not seem to be so great when one supposes (opinion, I have no concrete facts to serve as a basis).

For these reasons, it is not recommended to make this type of change directly in source codes if your only reason is to decrease the size. Keep your code clear and clean, and let other programs "enfeie" for you.

P.S. You also mention 'leave them more "secret", which I understood as an attempt to "protect the source code", right? If so, I suggest reading that my answer to a related question. Many people use obfuscators not with their original purpose of obfuscation, but rather because they perform better in the sense of reducing the size of the code.

    
12.06.2014 / 11:21