Skip to content

Instantly share code, notes, and snippets.

@cowboy
Created August 7, 2010 18:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cowboy/513071 to your computer and use it in GitHub Desktop.
Save cowboy/513071 to your computer and use it in GitHub Desktop.
Super Shrinkify
// This might be useful for certain size-limited JS competitions, or not. #js
function super_shrinkify( str ){
if ( str.length % 2 ) {
str += ' ';
}
var prefix = '"',
suffix = '".replace(/./g,function(a){a=a.charCodeAt();return String.fromCharCode(a>>7,a%128)})',
str_bytes = unescape( encodeURIComponent( str ) ).length,
out_bytes,
out = str.replace( /(.)(.)/g, function(a,b,c){
return String.fromCharCode( 128 * b.charCodeAt() + c.charCodeAt() );
});
out = prefix + out + suffix;
if ( eval(out) !== str ) {
console.log( 'error, input and decoded don\'t match!' );
console.log( 'input:\n' + '"' + str + '"' );
console.log( 'decoded:\n' + '"' + eval(out) + '"' );
} else {
out = 'eval(' + out + ')';
out_bytes = unescape( encodeURIComponent( out ) ).length;
console.log( 'before: ' + str.length + ' chars, ' + str_bytes + ' bytes' );
console.log( 'after: ' + out.length + ' chars, ' + out_bytes + ' bytes' );
console.log( 'savings: ' + ( str.length - out.length ) + ' chars, ' + ( str_bytes - out_bytes ) + ' bytes' );
}
return out;
};
// Usage:
super_shrinkify( already_minified_code_string );
// Example:
super_shrinkify('e=this,T=document,p=setInterval,J=T.body.style,m=T.getElementById("c"),V=m.getContext("2d"),g=Math,o=g.PI,E=o/180,X=g.min,l=g.sin,f=g.cos,D=g.pow,P=g.random,O="f001fa01ff0107010ff100f14081e8e".split(1),v=[],n=[];z=B=j=L=J.margin=0;Y=b=u=1;A=3;F=8;J.overflow="hidden";p(function(){I=m.width=e.innerWidth;R=m.height=e.innerHeight;t=X(k=I/2,U=R/2)-(F*2);for(Q=0;Q<33;Q++){N=v[Q]||(v[Q]={x:k,y:U});if(Q){S=v[Q-1];N.x+=(S.x-N.x)/(u+1);N.y+=(S.y-N.y)/(u+1)}}if(j<=1){z-=b*Y*4;H=l(q=z*E)*t;G=f(q)*t}else{z-=b*Y*2;H=l(q=z*E)*t;K=g.sqrt(D(H,2)+D(G=0,2));Z=g.atan2(G,H)+(q/j);H=K*f(Z);G=K*l(Z)}v[0].x=k+H;v[0].y=U+G;for(Q=0;Q<8;Q++){N=v[4*Q];n[B++%300]={s:1,d:1,c:O[Q],x:N.x,y:N.y}}V.fillRect(Q=0,0,I,R);while(N=n[Q++]){N.s+=N.d;N.d=N.s>=F?-1:N.s<=A?1:N.d;V.fillStyle="#"+N.c;V.beginPath();V.arc(N.x,N.y,N.s,0,o*2,false);V.fill()}},30);p(function(c,a){a=P();while(L==~~(c=P()*6)){}L=~~c;if(c<0.5){Y=-Y}else{if(c<2){O.push(O.shift())}else{if(c<3){j=a*7}else{if(c<4){b=a*8+1}else{if(c<5){u=a*3}else{F=a*10+6;A=X(F-4,P()*5+2)}}}}}},1024)');
// Console output:
before: 1024 chars, 1024 bytes
after: 603 chars, 1627 bytes
savings: 421 chars, -603 bytes
"eval("㊽㩨㓳ᙔỤ㟣㫭㋮㨬㠽㧥㩉㝴㋲㭡㘬┽⨮ㅯ㉹ᝳ㩹㙥᙭Ổᝧ㋴⋬㋭㋮㩂㳉㈨ᅣᄩᙖửᝧ㋴⇯㝴㋸㨨ᄲ㈢ᒬ㎽⛡㩨ᙯủᝐ⒬⊽㞯ᢸᠬⰽ㎮㛩㜬㘽㎮㧩㜬㌽㎮㇯㦬∽㎮㡯㮬⠽㎮㥡㝤㟭ᙏẢ㌰ᠱ㍡ᠱ㍦ᠱᠷᠱᡦ㌱ᠰ㌱ᨰᰱ㊸㊢ᝳ㡬㓴ᐱᒬ㬽ⷝ᙮ớ⺻㴽ℽ㔽☽┮㛡㥧㓮ẰᷙỢỵằ᷁ẳ᷆Ẹ᷊ᝯ㭥㥦㙯㮽ᅨ㓤㉥㜢ᷰᑦ㫮ㇴ㓯㜨ᓻⒽ㚮㯩㉴㐽㊮㓮㝥㥗㓤㩨᷒ửᝨ㋩㏨㨽㊮㓮㝥㥈㋩㏨㨻㨽Ⱘ㖽⒯᤬⪽⤯ᤩᚨ〉ᤩᷦ㟲ᑑẰ᷑ḳᦻ⢫ᖩ㷎Ỷⷑ⻼㸨㭛⣝ỻ㰺㖬㲺⫽ᒻ㓦ᑑᓻ⦽㭛⢭ᣝ᷎᝸ᖽᑓ᝸ᛎ᝸ᒯᑵᖱᒻ✮㲫Ẩ⦮㲭✮㲩ឨ㪫ᢩ㻽㓦ᑪḽᢩ㷺ᚽㄪⲪᨻ␽㘨㢽㴪⊩ᕴ᷇Ủᑱᒪ㩽㋬㧥㷺ᚽㄪⲪ᤻␽㘨㢽㴪⊩ᕴ᷋ủᝳ㣲㨨∨␬ᤩᗄᑇẰᘲᒩᷚủᝡ㩡㜲ᑇᙈᒫᑱ៪ᒻ␽▪㌨⴩᷇ịᕬᑚᓽ㭛ᡝ᝸ừᗈ᷶ⶰ⺮㲽⪫⎻㍯㤨⢽ᠻ⢼᰻⢫ᖩ㷎Ỷⶴᕑ⺻㝛Åᖥᦰᡝỻ㦺᢬㈺᢬ㆺ⟛⣝ᙸᵎ᝸ᙹᵎ᝹㻽⬮㍩㙬⥥ㇴᑑẰᘰᙉᙒᒻ㯨㓬㊨✽㝛⢫ᗝᓻ✮㦫Ỏᝤ᷎ᝤỎᝳἽ⌿ᚱᵎᝳḽ₿ᢺ✮㈻⬮㍩㙬⧴㳬㊽ᄣᄫ✮ㆻ⬮ㅥ㏩㝐ヴ㐨ᒻ⬮ヲㆨ✮㰬✮㲬✮㦬ᠬ㞪᤬㍡㙳㊩ᷖᝦ㓬㘨ᓽ㺬ᦰᒻ㠨㍵㝣㩩㟮ᑣᙡᓻソ⠨ᒻ㯨㓬㊨☽Ỿ㼨ㆽ⠨ᒪᬩᓻ㻌Ỿ㽣ᷩ㌨ㆼᠮ᪩㷙ậ⳽㋬㧥㷩㌨ㆼᤩ㷏ᝰ㫳㐨➮㧨㓦㨨ᒩ㻥㙳㋻㓦ᑣḳᓻ㔽オ᯽㋬㧥㷩㌨ㆼᨩ㷢ỡᔸᖱ㻥㙳㋻㓦ᑣḵᓻ㪽オ᧽㋬㧥㷆ỡᔱᠫᬻ₽Ⱘ⌭ᨬ⠨ᒪ᪫ᤩ㻽㻽㻽ᘱᠲᨩ".replace(/./g,function(a){a=a.charCodeAt();return String.fromCharCode(a>>7,a%128)}))"
@mathiasbynens
Copy link

This saves chars, not bytes, right?

@cowboy
Copy link
Author

cowboy commented Aug 8, 2010

Yeah, it actually adds bytes.. but the thought was that maybe entries are validated against a character limit, not a byte limit :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment