Please note that this question is intended to be a bit more on the theory side of the subject, but besides stripping whitespace what other techniques are used for JavaScript Compression?
views:
221answers:
4
A:
I can name a few that are found within Google Web Toolkit compiler:
- Inline of method calls
- Dead Code elimination
- Variable renaming/source obfuscation
- this means rewriting long variables to short ones and so on
Almost all of them require javscript parsing (ie they work beyond a pure lexical analysis).
Miguel Ping
2009-04-08 13:32:55
That dead code part scares me, how does GWT know that code is dead when JS has a global scope? Or is GWT designed for compiling all your JS at once, and doesn't work if you execute it on only one of your multiple JS files?
Daniel Lew
2009-04-08 13:38:17
GWT is a monolithic compiler, so by adding some restrictions to what you can do within java, it can accurately determine if a method is called or not (similar to what you can do with eclipse ide).
Miguel Ping
2009-04-08 13:42:38
+1
A:
Off the top of my head...
- Tokenizes local variables and then renames them to a minimally-sized variable.
- Removes tons of whitespace.
- Removes unnecessary braces (for example, single line executions after if-statements can remove braces and add a single semi-colon).
- Removes unnecessary semi-colons (for example, right before an ending brace '}').
My most commonly used minifier is the YUI Compressor, and as they state, it's open source so you can take a look for yourself on exactly what they do. (I'm not sure what they mean by "micro-optimizations", probably a bunch of rare cases that gain you a character or two.)
Daniel Lew
2009-04-08 13:33:15
+1
A:
Most of the compressors use a combination of different techniques:
- stripping whitespaces
- compress file by compression algorythm (gzip, deflate)
- most of the space is saved renaming interall variables and function to shorter names, eg:
This function:
function func (variable) {
var temp = 2 * variable;
return temp;
}
will become:
function func (a) {
var b = 2 * a;
return b;
}
- Dean Edwards packer uses some internal compression. The script is decompressed whhen loaded on the page.
- All the usual stuff to make a programmcode shorter:
- delete unused code
- function inlining
Hippo
2009-04-08 13:34:57
if you want to use func on global javascript scope i would leave the name as it is... ;-)
Hippo
2009-04-08 15:37:46
A:
Code renaming and reordering so that gzip compressor will have better result.
for example (not so clever)
original code:
function mul(mul1, mul2)
{
return mul1 * mul2;
}
function print(str)
{
// do something
}
function add(add1, add2)
{
return add1 + add2;
}
modify code:
function mul(a,b)
{
return a * b;
}
function add(a, b)
{
return a + b;
}
function print(str)
{
// do something
}
Shay Erlichmen
2009-04-08 14:54:13
I'm down voting this answer as you didn't really answer the question, namely what *techniques* are being used.
Rob
2009-04-08 15:00:05
sorry Rob, I didn't read the entire question. New answer, hope I do better this time :)
Shay Erlichmen
2009-04-08 15:04:17
It's just a sample to the idea, of course that you can preform only operation that won't affect the result.
Shay Erlichmen
2009-04-08 15:26:49