Ever since I started working on the mostly Ajax app “VKitty“, I’ve been looking into JavaScript compression, and trying to figure out how it works. Most compressors have very simple ways to compress JavaScript, by simply removing whitespace, but Dean Edwards’s /packer/ defies the simplicity of most compressors by making an unreadable function that acts the same as the JavaScript you feed it, also known as obfusication.
I’ve decided to find out how it works, and I’m starting with something very simple. “alert(’STUFF GOES HERE’)”.
First, I packed it in /packer/. I got back the code “eval(function(p,a,c,k,e,r){e=String;if(!”.replace(/^/,String)){while(c–)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return’\\w+’};c=1};while(c–)if(k[c])p=p.replace(new RegExp(’\\b’+e(c)+’\\b’,'g’),k[c]);return p}(’0(\’1 2 3\’)',4,4,’alert|STUFF|GOES|HERE’.split(’|'),0,{}))”, which is really unreadable.
Let’s unpack the /packer/.
I did a bit of formatting on the function returned by /packer/: (it’s an image because I couldn’t get syntax highlighting to… Visit the blog to read more of Unpacking /packer/