Javascript String Compression

I am trying to introduce a javascript contest where the script should be <= 1kb in size. Minifying and eval are allowed, so I run it through the google close compiler (which is slightly better than any others I tried).

But I found that if I convert the script to a string and replace long words like "function" and "return" with individual characters, I can reduce this even further. Then, inserting the line into my script, doing the substitution to restore it, and then “evaling”, I can get the original behavior.

So I was wondering if I can generalize this last method. Has anyone seen or written code to compress / decompress strings this way? Feel how to think about it?

EDIT To make myslelf clear, I am asking about compression and decompression of lines in javascript - not minimization. For example. how to find the most common patterns in a string and how to write a tiny decompressor in javascript for strings where these occurrences have been replaced with single characters.

Thanks.

+7
javascript string compression
source share
3 answers

Are you looking for http://www.iteral.com/jscrush/ ? I found it useful for the same competition (I assume it is js1k).

+3
source share

Have you considered code shortening by creating a shortcut for these JavaScript objects and methods that you use in your code:

var d = document; var id = d.getElementById; 

And then instead of writing

 document.getElementById("foo") 

You can write

 id("foo"); 
+2
source share

Tokenisation is the preferred script compression method because it works with individual keywords and other names.

0
source share

All Articles