Skip to content

Instantly share code, notes, and snippets.

@aemkei
Created August 18, 2010 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aemkei/534021 to your computer and use it in GitHub Desktop.
Save aemkei/534021 to your computer and use it in GitHub Desktop.
/*
* #js1k - JavaScript Encoder / Decoder
*
* Simply paste your code and shrink it down to about 50%.
* This algorithm uses Bit-Shifting to pack two chars into one.
* The encoder is included in the output and weights only 89 bytes.
*
* Note: This will reduce the character count only, but not the file size!
*
* by Martin Kleppe <kleppe@gmail.com>
* http://twitter.com/aemkei
*/
var code = [
'for(i=x="",f=String.fromCharCode;c="',
'".charCodeAt(i++);)x+=f((c>>7)%128)+f(c%128);eval(x)'
];
function compress(x){
if (!x.length%2) { x+= " "; }
for(i=y="",f="charCodeAt",x+=" ";(a=(x[f](i++)||0)<<7)&&(b=x[f](i++)||0);)y+=String.fromCharCode(a+b);return y
}
function decompress(y){
for(i=x="",f=String.fromCharCode;c=y.charCodeAt(i++);)x+=f((c>>7)%128)+f(c%128);return x
}
function $(tag){
return document.getElementsByTagName(tag)[0];
}
function run(){
var textarea = $("textarea"),
input = textarea.value,
output = input.indexOf(code[0]) != 0 ?
code[0] + compress(input) + code[1] :
decompress(input.replace(code[0], "").replace(code[1], ""));
textarea.value = output;
$("span").innerHTML = output.length + " / " + input.length + " bytes. " +
Math.round((output.length/input.length)*100) + "%";
}
function sample(){
$("textarea").value = source;
}
document.body.innerHTML =
'<style type="text/css">' +
'body,button{font-family:Helvetica;line-height:1.3em;font-size:1em;}' +
'textarea{margin:1em 0;}' +
'</style>' +
'<h1>JavaScript Encoder</h1>' +
'<b>Paste your own script:</b><br/>' +
'<textarea cols="100" rows="20">' + source + '</textarea><br/>' +
'<button onclick="run()">De/Encoder</button> ' +
'<span>Hit the button!</span><br/><br/>' +
'<a href="#" onclick="sample()">Load source</a>';
// Wow, what a hack!
// same file as above
for(i=x="",f=String.fromCharCode;c="ឪညဪဣ㕳ᣫိ၊ヶビㇲ㓰㨠⋮㇯㉥㤠ហ≥㇯㉥㤊ဪညဪၓ㓭㡬㲠㡡㧴㊠㳯㫲ၣ㟤㊠ヮ㈠㧨㥩㝫ၩ㨠㉯㯮ၴ㞠モ㟵㨠᪰ኮԠᔠ⩨㓳ၡ㙧㟲㓴㑭ၵ㧥㦠Ⅹ㨭⧨㓦㩩㝧ၴ㞠㡡㇫ၴ㯯ၣ㑡㥳ၩ㝴㞠㟮㊮Ԡᔠ⩨㊠㋮㇯㉥㤠㓳ၩ㝣㙵㉥㈠㓮ၴ㑥ၯ㫴㡵㨠ヮ㈠㯥㓧㑴㦠㟮㙹းᲠㅹ㩥㦮ԠᔠԠᔠ❯㩥ᴠ⩨㓳ၷ㟲㗳ၦ㟲ၳ㟵㥣㊠㇯㉥ၵ㧩㝧၁⧃Ⓣၣ㑡㥡ㇴ㋲㦠㟮㙹ႊဪညဪၢ㲠⛡㥴㓮။㙥㡰㊠ṫ㙥㡰㋀㏭ラ㘮㇯㚾Ԡᔠ㑴㩰ᴯ៴㯩㩴㋲ᝣ㟭១㋭㗥㒊ဪដնヲၣ㟤㊠ẠⶊဠᏦ㟲ᑩỸẢᄬ㌽⧴㥩㝧ᝦ㥯㛃㑡㥃㟤㊻ㆽᄧᘊဠᎢᝣ㑡㥃㟤㋁㨨㒫ᖩᶩ㰫Ủᐨㆾἷᒥᢲᰩᗦᑣ኱ᤸᒻ㋶レᑸᒧ՝ᶊզ㫮ㇴ㓯㜠㇯㛰㥥㧳ᑸᓻԠၩ㌠ᐡ㰮㙥㝧㩨ኲᒠ㶠㰫ẠᄠᄻၽԠၦ㟲ᑩỹẢᄬ㌽ᅣ㑡㥃㟤㋁㨢ᙸᖽᄠᄻᑡẨ㱛㍝ᑩᖫᓼ㸰ᒼḷᒦጨㄽ㱛㍝ᑩᖫᓼ㸰ᒻᓹᖽ⧴㥩㝧ᝦ㥯㛃㑡㥃㟤㊨カㄩᷲ㋴㫲㜠㲊㺊զ㫮ㇴ㓯㜠㉥㇯㛰㥥㧳ᑹᓻԠၦ㟲ᑩỸẢᄬ㌽⧴㥩㝧ᝦ㥯㛃㑡㥃㟤㊻ㆽ㲮㇨ヲ⇯㉥⃴ᑩᖫᒻᓸᖽ㌨ᑣἾᮩ኱ᤸᒫ㌨ㆥᢲᰩᷲ㋴㫲㜠㰊㺊զ㫮ㇴ㓯㜠ረ㩡㎩㶊ဠ㥥㩵㥮ၤ㟣㫭㋮㨮㏥㩅㙥㛥㝴㧂㳔ョ❡㛥ᑴョᓛᡝᶊ㺊զ㫮ㇴ㓯㜠㥵㜨ᓻԠၶヲၴ㋸㩡㥥゠Ạረᅴ㋸㩡㥥アᒬညဠဠ㓮㡵㨠Ạ㩥㱴ヲ㋡᝶レ㫥ᘊဠဠ㟵㩰㫴ွၩ㝰㫴ᝩ㝤㋸⟦ᑣ㟤㋛ᡝᒠႽူဿညဠဠဠ㇯㉥ⶰ⺠ᖠ㇯㛰㥥㧳ᑩ㝰㫴ᒠᖠ㇯㉥ⶱ⺠ᴠԠဠဠၤ㋣㟭㡲㋳㦨㓮㡵㨮㥥㡬ャ㊨㇯㉥ⶰ⺬ဢᄩᝲ㋰㙡㇥ᑣ㟤㋛ᣝᘠᄢᒩᶊဠԠၴ㋸㩡㥥ギ㭡㙵㊠Ạ㟵㩰㫴ᶊԠဤᐢ㧰ヮᄩᝩ㝮㋲⑔⛌ွၯ㫴㡵㨮㙥㝧㩨ါဢုဢါၩ㝰㫴ᝬ㋮㏴㐠ᖠᄠㅹ㩥㦮ဢါԠဠ၍ヴ㐮㥯㫮㈨ᑯ㫴㡵㨮㙥㝧㩨៩㝰㫴ᝬ㋮㏴㐩ᔱᠰᒠᖠᄥᄻսԊ㍵㝣㩩㟮ၳロ㡬㊨ᓻԠဤᐢ㩥㱴ヲ㋡ᄩ᝶レ㫥ွၳ㟵㥣㊻սԊ㉯ㇵ㛥㝴ᝢ㟤㲮㓮㝥㥈⩍☠ẠԠဧṳ㩹㙥ၴ㳰㊽ᅴ㋸㨯ㇳ㦢ἧါԠဧㅯ㉹ᙢ㫴㩯㝻㍯㝴ᛦロ㓬㲺⑥㙶㋴㓣セ㙩㝥ᛨ㋩㏨㨺᢮᧥㚻㍯㝴ᛳ㓺㊺ᣥ㚻㺧ါԠဧ㩥㱴ヲ㋡㷭ヲ㏩㜺ᣥ㚠ᠻ㺧ါԠဧḯ㧴㳬㊾ᎠᖊဠᎼ㐱Ὂヶビㇲ㓰㨠⋮㇯㉥㤼៨ᢾᎠᖊဠᎼㄾ⡡㧴㊠㳯㫲ၯ㯮ၳㇲ㓰㨺ḯㄾṢ㤯ἧါԠဧṴ㋸㩡㥥゠㇯㙳Ảᢰᠢၲ㟷㦽ᄲᠢἧါၳ㟵㥣㊠ᖠᎼ៴㋸㩡㥥ゾṢ㤯ἧါԠဧṢ㫴㩯㜠㟮㇬㓣㖽ᅲ㫮ᐩᄾ≥ៅ㝣㟤㋲ḯㅵ㩴㟮ἠᎠᖊဠᎼ㧰ヮὈ㓴ၴ㑥ၢ㫴㩯㜡ḯ㧰ヮἼㅲើṢ㤯ἧါԠဧṡၨ㥥㌽ᄣᄠ㟮㇬㓣㖽ᅳロ㡬㊨ᒢὌ㟡㈠㧯㫲㇥ḯゾᎻԠညឯၗ㟷ᘠ㯨ヴၡၨャ㖡Ԡ".charCodeAt(i++);)x+=f((c>>7)%128)+f(c%128);eval(x)
@aemkei
Copy link
Author

aemkei commented Aug 18, 2010

Note: This will only reduce the character count but INCREASE the bytes!
When you try to submit this to #js1k you'll see this message:
Script length exceeds limit. The limit is 1024 bytes (not characters!).
Yours is 2785 bytes (and 1016 characters).
Note that higher utf-8 and utf-16 characters take up 2 to 4 bytes each, depending on position.

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