The idea behind Minification and Obfuscation is very simple. Lets consider following two examples. In first call the client has requested a file of 3.4 MB. In the second example the client has requested a 2 MB file. With little knowledge about web applications anyone can tell that the time taken will be less in case of second example considering all the environmental factors are similar.
It make it clear that small files = less time.
Obfuscation is technique of optimizing source code. Like minification, it removes comments and whitespace. On top of minification obfuscation also munges the code. As part of munging, function and variable names are converted into smaller strings making the code more compact, as well as harder to read. This is typically done to make it more difficult to reverse-engineer the code. But munging can help performance because it reduces the code size beyond what is achieved by minification.
Minification or Obfuscation
What should we should choose to optimize code, minification or obfuscation.
- Minification is a safe and fairly straightforward process. Obfuscation, on the other hand, is more complex.
- Because obfuscation is more complex, there’s a higher probability of introducing errors into the code as a result of the obfuscation process itself.
- Obfuscated code is more difficult to read. This makes issue debugging difficult in production environment.
Following image shows two scripts of different sizes, 50K and 401K, and effect of minification and obfuscation on them.
We can conclude following points from the above figure:
- The smaller script of size 50K was reduced to 13K and 12K respectively after minification and obfuscation. The file download time reduced to 481ms and 471ms respectively after minification and obfuscation when compared to 581ms time of normal file size download time.
- The bigger script of size 401K was reduced to 131K and 128K respectively after minification and obfuscation. This is significant reduction in size. The file download time reduced to 769ms and 755ms respectively after minification and obfuscation when compared to 1112ms time of normal file size download time.
- Obfuscation gives better results in both cases. It further reduces the size then minification and thus takes less time.
- Minifying scripts reduces response times without carrying the risks that come with obfuscation.
Best of way out
- Inline Scripts
- Gzip and Minification