Enter the Javascript source code you would like to compress
The following variable redefinitions have been performed
Customise the level of compression
Remove comments
Remove white space
Remove line terminators
Rename variables
To leave specific variable names unaffected enter them, one per line, in the text area below:
Getting started:
Go to the input tab and enter the javascript code you wish to compress in the text area. When you are
ready, click the 'Compress' button in the top right.
Once the compression of your code has been completed it will be displayed on the output tab. At the top
there is also a summary of the compression ratio that has been achieved.
The variables and functions that have been renamed in your javascript are summarised on the dictionary tab.
To compress further javascript using the same dictionary, return to the input tab and repeat the above procedure.
It is necessary to use the same dictionary if the second piece of javascript is to be used on the same page as the first.
If you wish to start afresh with a new dictionary it can be cleared with the 'Clear' button in the top
right of the dictionary tab.
The are known issues with the following:
Regular expression literals
Expressions containing regular expression literals such as
var regexp = /^gr[ae]y$/i;
will be misinterpreted by the compressor. It is recommended that for
now you either replace regular expression literals with RegExp objects, e.g.
var regexp = new RegExp('gr[ae]y', 'i');
or alternatively could temporarily replace your regexp with a string, i.e.
var regexp = '/^gr[ae]y$/i';
perform the compression, then return the strings to regular expression literals.
Automatic semicolon insertion
Javascript will automatically insert semicolons in certain situations. For instance, if an unexpected
element is encountered after a new line a semicolon will be automatically inserted before the offending
element. For example in the code
a = 1
b = 2
When javascript encouters the unexpected token 'b' it will automatically insert a semicolon before it
giving the valid javascript
a = 1
;b = 2
The javascript compressor will however remove the new lines without inserting the required semicolons
and as such result in badly formed javascript such as
a = 1b = 2
So don't rely on automatic semicolon insertion. Note, you probably use automatic semicolon insertion
without realising, for example, the following piece of code
CClass.prototype.MemberFn1 = function()
{
// do something
}
CClass.prototype.MemberFn2 = function()
{
// do something else
}
When javascript parses the above, it automatically inserts a semicolon before the second member function.
Some automatic semicolon insertion is handled and even taken advantage of by the compressor. Automatic
semicolon insertion in restricted productions of the Javascript grammar are handled by the compressor.
For example
a
;++b
and similarly for the "--" operator, also for
return
;c
and similarly for "break", "continue" and "throw" statements.
Javascript will also automatically insert semicolons before unexpected close braces "}". As such
semicolons before close braces are removed by the Javascript compressor.
The Javascript Compressor was written in Javascript. Clearly this is not the most appropriate language for writing
such an application, it was more just a fun exercise that I ended up doing after reading the ECMA script specification!
Writing it in Javascript does, however, allow you to take a couple of shortcuts using the eval statement.