Javascript compression techniques and tips

Asked

Viewed 163 times

2

Recently I found for case another way to use the true and the false replacing them with !0 and !1. I kept thinking about other ways to compress the codes or make them more "secret" but I found nothing on the internet talking about it, only giving links to javascript compressors.

I even tried to take a look at JS bookstores, but you know, you can’t understand anything, so I wanted to know what 'techniques' or tips you use to decrease the size of codes.

  • 3

    My suggestion is to even use a library that does this. Doing it by hand is risky, it can give errors. There are many details to take into account like true === 1 // false.

  • 1

    If for you the fact !0 result in false is something secret, you need to review your concepts.

  • 1

    not everyone knows it @Renan ..

1 answer

7

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 why people use Javascript mini-protectors (or obfuscators) is precisely because this manual work is unnecessary, repetitive and subject to errors:

  • Your code gets harder to read;
  • Your code gets harder to debug (when seeing an error message, it is harder to locate it in the sources);
  • Simple errors (such as exemplified by Sergio in the comments), that would be immediately visible in a "clean" code could go unnoticed in a code that uses many such tricks.
  • Often the process of minimizing code is "donkey" (e.g., exchanging all variables of many letters for variables of one letter), so it is better to delegate this task to a tool than to do it yourself.
    • And, in addition, this tool can do things that you wouldn’t think of doing in the original source code (e.g.: remove all line breaks and indentations).
  • And in the end, most webservers compresses contents (using gzip or deflate) before sending to customers, so that even the gains from minification do not seem to me be so great when it is supposed (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 the sources if its only reason is to decrease the size. Keep your code clear and clean, and let other programs "enfeie" you for you.

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

  • I understood what you mean, I’ve always used these 'obfuscation' techniques and never had problems finding bugs or some error, I don’t take it to the max (like leaving all the code in one line) but I like to use new techniques and make it difficult for other people to understand or simply to dress it up. But really, I ended up agreeing with your answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.