Blog

JavaScript Minification

JavaScript code is written for, and by humans, who need whitespace, formatting, and comments to be able to understand and debug the code. Code minification is the process of removing all unnecessary characters from the source code. The main idea is to reduce payload sizes and script parse time. Code minification does not affect the functionality of the source code. This is because browsers can execute code without needing to understand it. It however, improves load time because the size of the file to be downloaded now reduces.

# Obfuscation, compression, or uglification

  • Obfuscation is usually used to hide business logic. The code is modified such that it becomes unreadable by humans. This makes reverse engineering difficult. Obfuscation is different from encryption in that computers are still able to understand and execute the code. Obfuscation is accomplished by changing the names of variables, functions, and members. The resulting reduction in file size also improves performance, though this is not the primary goal of obfuscation.

  • Compression is a process that reduces the number of bits needed to represent data. Data compression can free up valuable space on hard drives, speed up file transfer, and decrease costs for network bandwidth. Some files, like Microsoft Word files, may be compressed to 90 percent of their original size.

  • To be honest I don't find a difference between uglification and minification. They are essentially the same. To 'uglify' a JavaScript file is to minify it using Uglify. Uglification improves performance while reducing readability.

# Are there any disadvantages?

  • It happens that minification can break complicated scripts because of site-dependent variables like themes, plugins, and server environment. Also minification can also introduce errors that are hard to debug. It is because the minified JavaScript is just one big line of code without any whitespaces or new lines in between and if there is an error the developers tool just say that problem is in line 1 and it is impossible to say which part of that big compressed 1 line is causing the problem.

  • When the file is written by somebody else, it is hard to understand the logic because the variables names are changed to single letter. Although some may consider this as an advantage, because it doesn't allow another person to view what you have done.

# Conslusion

What I suggest? It is always better to have a backup copy of your JavaSCript before minification.

Image placeholder

V. Stefanova

Engineer Front-end developer