Search code examples
javascriptminify

How does a JavaScript engine understand minified JavaScript scripts?


Usually, if plaintext is compressed, there must be decompression routine. How does a JavaScript engine interpret minified compressed JavaScript scripts?

Does a JavaScript engine have built-in deminification algorithms?


Solution

  • First, you need to understand that there is a difference between minification and compression.

    1. Minification does not compress

    Minification is the process of reducing JavaScript to as few bytes as possible, by removing extra whitespace, changing variable names to shorter ones, etc. The encoding and characters of the file remain the same. Since a minified file doesn't actually change the encoding or functionality, nothing is needed to convert a file back since nothing really changed.

    2. Compression changes a file

    When you compress a file, say using gzip, you are re-encoding the data of a file or stream into a different encoding that takes up less space. It is in this instance where a decompression routine is needed to translate the file back to its uncompressed state. When uncompressed, the file returns to its original state.

    3. Browsers use a combination of compression and minifcation to achieve as small of a bandwidth footprint as possible.

    What's great about minification and compression is that they are two separate processes that do two separate things, and they can be combined to deliver as small a file to the browser as possible. For example, the original jQuery source right now is well over 200 KB, but through minifcation and delivering the file compressed, it only takes ~30 KB of bandwidth to deliver to the browser.