I'm trying to enter a javascript petition where the script has to be <= 1kb in size. Minifying and eval is allowed, so I've run it through google's closure piler (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 press/depress strings in this way? Feel like thinking about it?
EDIT To make myslelf clear, I'm asking about pressing and depressing strings in javascript - not minifying. E.g. how to find the most mon patterns in a string, and how to write a tiny depressor in javascript for strings where these occurences have been replaced with single chars.
Thanks.
I'm trying to enter a javascript petition where the script has to be <= 1kb in size. Minifying and eval is allowed, so I've run it through google's closure piler (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 press/depress strings in this way? Feel like thinking about it?
EDIT To make myslelf clear, I'm asking about pressing and depressing strings in javascript - not minifying. E.g. how to find the most mon patterns in a string, and how to write a tiny depressor in javascript for strings where these occurences have been replaced with single chars.
Thanks.
Share Improve this question edited Aug 18, 2010 at 13:34 sje397 asked Aug 15, 2010 at 3:56 sje397sje397 41.9k9 gold badges89 silver badges106 bronze badges 5- 1 Erm... maybe Dean Edward's /packer/? – Yi Jiang Commented Aug 15, 2010 at 4:01
- 1 pressorrater.thruhere - a helpful tool for paring output from different minifiers or pression methods – Cristian Sanchez 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 pressing/depressing strings - it just happens that my string is minified js. – sje397 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 press more easily: alistapart./articles/better-javascript-minification alistapart./articles/javascript-minification-part-II – Jenni 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 pressing/depressing inside the script. – sje397 Commented Aug 18, 2010 at 13:54
3 Answers
Reset to default 3Do you happen to be looking for http://www.iteral./jscrush/ ? I found it useful for the same petition (I assume it is js1k).
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");
Tokenisation is the preferred method for pressing scripts as it works with the individual keywords and other names.