Javascript Compressor

You can compress & obfuscate your javascript files with this free online javascript compressor. Compressing your scripts will make them download faster, reduce bandwidth usage and make it harder for others to steal your code. The javascript compressor leaves function names and global variables untouched, so your script will continue to work with other scripts.

New Features Added 25/05/2007

The javacript compressor now supports real compression for even better compression ratios! You can still get the same results you were getting previously by checking the 'Crunch Only' option.

Javascript Compressor Input


Crunch Only

Javascript Compressor Output


Javascript Compressor Results

InputOutputCompression
Size

Why use it

Compressing your javascript with this online tool has two advantages. The first is that by reducing the size of your javascript file you reduce both the time taken for people to download your script as well as reducing the bandwidth used in transferring the script. When I used the compression script to compress itself the size of the script was reduced from 3597 Bytes to 1942 Bytes, a saving of almost 50%!

The second is that compressing your script also makes it harder for anyone who wants to use your script without permission to understand how it works. As an example of this try interpreting the javascript compressor source code without reading the explanation below. You can never stop people from getting your source code but compressing it makes it very hard to work with.

How it works

At a high level, the javascript compressor works by removing all comments, unnecessary white-space and shortening all local variable names to a single character.

To do this it goes runs through the following steps.

  1. Preserve Strings - All strings in the script are stored and then removed from the script.
  2. Strip Comments - All comments in the script are removed.
  3. Replace Local Variable Names - All local variables within functions are replaced with single character variables.
  4. Preserve Reserved Words - Reserved words that require a trailing space are stored and then removed from the script.
  5. Strip White-space - All whitespace is removed from the script.
  6. Restore Reserved Words - The reserved words stored earlier are added back to the script.
  7. Restore Strings - The strings stored earlier are added back to the script.

Preserve Strings

The javascript compressor removes all unneeded whitespace from the input script. To ensure that strings are left untouched they are all stored and replaced with a unique identifier. When the compression is complete these identifiers are replaced with the original strings.

To identify and store the strings the javascript compressor uses regular expressions. First a regular expression is declared that will match any string surrounded by single or double quotes. Then all the matches are stored using the match function of the String object. All the strings are then replaced with zzstringzz using the replace function of the String object.

Strip Comments

Removing the comments is also done with regular expressions and the String replace function. 2 regular expressions are required, one for single line comments denoted by 2 forward slashes (//), another for multiline comments surrounded by a forward slash star pair (/*...*/).

Replace Local Variable Names

This is the most convoluted part of the javascript compressor. First the compressor goes looking for the first instance of the function keyword. Everything before this is global and left untouched. Once the function keyword has been found the script is split into 2 parts using via a loop and regular expressions. The first contains just the function and the second contains everything in the script after the function. The function is identified by finding the matching close bracket for the function using regular expressions. The function finding code is then called recursively on the remaining part of the script to identify any other functions in the script.

Having identified the individual functions the javascript compressor can now replace the local variable names. The first ones replaced are the function parameters. A regular expression is constructed from the parameter names and then used to replace all instances of the parameter with a unique single character variable name.

Local variables are then identified as anything within the function preceded with the var keyword. Again a regular expression is constructed and used to replace all instaces of the parameter with a unique single character variable name.

Preserve Reserved Words

Before whitespace can be stripped from the script the javascript compressor must identify keywords used that require a trailing space. These are stored via the String match function and then replaced with an indentifier.

Strip White-space

All whitespace in the script is removed using the String replace function and a regular expression.

Restore Reserved Words

The reserved words stored earlier are put back into the script in the positions marked by the identifier they were replaced with.

Restore Strings

The strings stored earlier are put back into the script in the positions marked by the identifier they were replaced with.