3

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?

1
  • minification just shortens variable names, eliminates as much whitespace as possible, takes out comments, etc... Anything to make the JS smaller, but still be syntactically valid. Basically it'd take var this_is_a_really_long_useless_var_name=0; and turn it into var x=0; Commented May 25, 2011 at 18:30

4 Answers 4

6

It does not need to be decompressed; minified code is still JavaScript. It’s just harder for humans to read.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

2

Minified JavaScript code is still javascript. Think of it as removing the white space and renaming long variable to shorter variable.

2 Comments

so, basically, minification is not compression at all, when compared to compression algorithms like LZW or Huffman coding??
No it's not compression. It's simply stripping whitespace and renaming variables. Some tools have optimization as well, check Closure Compiler.
1

Minified JavaScript is simply that same code, but abbreviated (e.g. var foobar = 2 may before var a=2;)

GZipped JavaScript files (and other static files) are genuinely compressed, and do indeed get decompressed by the client machine before the engine uses them.

The browser will identify to the server that it can access zipped content by a header similar to

Accept-Encoding: gzip, deflate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.