7

I'm trying to enter a javascript competition where the script has to be <= 1kb in size. Minifying and eval is allowed, so I've run it through google's closure compiler (which does slightly better than any others I've tried).

But I've found that if I convert the script to a string, and replace long words like 'function' and 'return' with single chars, I can reduce that even further. Then, by embedding the string in my script, performing the substitution to restore it, and then 'evaling' it, I can get the original behaviour.

So I was wondering if I could generalise this last method. Has anyone seen or written code to compress/decompress strings in this way? Feel like thinking about it?

EDIT To make myslelf clear, I'm asking about compressing and decompressing strings in javascript - not minifying. E.g. how to find the most common patterns in a string, and how to write a tiny decompressor in javascript for strings where these occurences have been replaced with single chars.

Thanks.

5
  • 1
    Erm... maybe Dean Edward's /packer/? Commented Aug 15, 2010 at 4:01
  • 1
    compressorrater.thruhere.net - a helpful tool for comparing output from different minifiers or compression methods Commented Aug 15, 2010 at 4:10
  • None of the minifiers I've tried work as well as Google's (including packer), which I'm already using. The question is about compressing/decompressing strings - it just happens that my string is minified js. Commented Aug 16, 2010 at 2:29
  • 2
    maybe you've already read these, but here are two recent articles from A List Apart on how to write code that minifiers can compress more easily: alistapart.com/articles/better-javascript-minification alistapart.com/articles/javascript-minification-part-II Commented Aug 18, 2010 at 13:37
  • @Jenni - cheers, I will read that. I actually got my script down to an acceptable size. But I'm still interested in this idea of compressing/decompressing inside the script. Commented Aug 18, 2010 at 13:54

3 Answers 3

3

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

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

2 Comments

Nope. That's another minifier. I'm looking to compress and decompress strings.
Apologies. That's exactly the sort of thing I was talking about.
2

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

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

And then instead of writing

document.getElementById("foo")

You can write

id("foo");

1 Comment

Yep, done that already...but also, the closure compiler does it as part of it's optimisation as well.
0

Tokenisation is the preferred method for compressing scripts as it works with the individual keywords and other names.

1 Comment

Most of this is done by the closure compiler - there are very few javascript keywords I'm using (function, return etc) but the closure compiler automatically makes all function names just 1 letter long, etc.

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.