Created
August 18, 2010 08:31
-
-
Save aemkei/534021 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* #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! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// same file as above | |
for(i=x="",f=String.fromCharCode;c="ឪညဪဣ㕳ᣫိ၊ヶビㇲ㓰㨠⋮㉥㤠ហ≥㉥㤊ဪညဪၓ㓭㡬㲠㡡㧴㊠㳯㫲ၣ㟤㊠ヮ㈠㧨㥩㝫ၩ㨠㉯㯮ၴ㞠モ㟵㨠᪰ኮԠᔠ⩨㓳ၡ㙧㟲㓴㑭ၵ㧥㦠Ⅹ㨭⧨㓦㩩㝧ၴ㞠㡡ၴ㯯ၣ㑡㥳ၩ㝴㞠㟮㊮Ԡᔠ⩨㊠㋮㉥㤠㓳ၩ㝣㙵㉥㈠㓮ၴ㑥ၯ㫴㡵㨠ヮ㈠㯥㓧㑴㦠㟮㙹းᲠㅹ㩥㦮ԠᔠԠᔠ❯㩥ᴠ⩨㓳ၷ㟲㗳ၦ㟲ၳ㟵㥣㊠㉥ၵ㧩㝧၁⧃Ⓣၣ㑡㥡ㇴ㋲㦠㟮㙹ႊဪညဪၢ㲠⛡㥴㓮။㙥㡰㊠ṫ㙥㡰㋀㏭ラ㘮㚾Ԡᔠ㑴㩰ᴯ៴㯩㩴㋲ᝣ㟭១㋭㗥㒊ဪដնヲၣ㟤㊠ẠⶊဠᏦ㟲ᑩỸẢᄬ㌽⧴㥩㝧ᝦ㥯㛃㑡㥃㟤㊻ㆽᄧᘊဠᎢᝣ㑡㥃㟤㋁㨨㒫ᖩᶩ㰫Ủᐨㆾἷᒥᢲᰩᗦᑣᤸᒻ㋶レᑸᒧ՝ᶊզ㫮ㇴ㓯㜠㛰㥥㧳ᑸᓻԠၩ㌠ᐡ㰮㙥㝧㩨ኲᒠ㶠㰫ẠᄠᄻၽԠၦ㟲ᑩỹẢᄬ㌽ᅣ㑡㥃㟤㋁㨢ᙸᖽᄠᄻᑡẨ㱛㍝ᑩᖫᓼ㸰ᒼḷᒦጨㄽ㱛㍝ᑩᖫᓼ㸰ᒻᓹᖽ⧴㥩㝧ᝦ㥯㛃㑡㥃㟤㊨カㄩᷲ㋴㫲㜠㲊㺊զ㫮ㇴ㓯㜠㉥㛰㥥㧳ᑹᓻԠၦ㟲ᑩỸẢᄬ㌽⧴㥩㝧ᝦ㥯㛃㑡㥃㟤㊻ㆽ㲮ヲ⇯㉥ᑩᖫᒻᓸᖽ㌨ᑣἾᮩᤸᒫ㌨ㆥᢲᰩᷲ㋴㫲㜠㰊㺊զ㫮ㇴ㓯㜠ረ㩡㎩㶊ဠ㥥㩵㥮ၤ㟣㫭㋮㨮㏥㩅㙥㛥㝴㧂㳔ョ❡㛥ᑴョᓛᡝᶊ㺊զ㫮ㇴ㓯㜠㥵㜨ᓻԠၶヲၴ㋸㩡㥥゠Ạረᅴ㋸㩡㥥アᒬညဠဠ㓮㡵㨠Ạ㩥㱴ヲ㋡レ㫥ᘊဠဠ㟵㩰㫴ွၩ㝰㫴ᝩ㝤㋸⟦ᑣ㟤㋛ᡝᒠႽူဿညဠဠဠ㉥ⶰ⺠ᖠ㛰㥥㧳ᑩ㝰㫴ᒠᖠ㉥ⶱ⺠ᴠԠဠဠၤ㋣㟭㡲㋳㦨㓮㡵㨮㥥㡬ャ㊨㉥ⶰ⺬ဢᄩᝲ㋰㙡ᑣ㟤㋛ᣝᘠᄢᒩᶊဠԠၴ㋸㩡㥥ギ㭡㙵㊠Ạ㟵㩰㫴ᶊԠဤᐢ㧰ヮᄩᝩ㝮㋲⛌ွၯ㫴㡵㨮㙥㝧㩨ါဢုဢါၩ㝰㫴ᝬ㋮㏴㐠ᖠᄠㅹ㩥㦮ဢါԠဠ၍ヴ㐮㥯㫮㈨ᑯ㫴㡵㨮㙥㝧㩨៩㝰㫴ᝬ㋮㏴㐩ᔱᠰᒠᖠᄥᄻսԊ㍵㝣㩩㟮ၳロ㡬㊨ᓻԠဤᐢ㩥㱴ヲ㋡ᄩレ㫥ွၳ㟵㥣㊻սԊ㉯ㇵ㛥㝴ᝢ㟤㲮㓮㝥㥈⩍☠ẠԠဧṳ㩹㙥ၴ㳰㊽ᅴ㋸㨯ㇳ㦢ἧါԠဧㅯ㉹ᙢ㫴㩯㝻㍯㝴ᛦロ㓬㲺⑥㙶㋴㓣セ㙩㝥ᛨ㋩㏨㨺᧥㚻㍯㝴ᛳ㓺㊺ᣥ㚻㺧ါԠဧ㩥㱴ヲ㋡㷭ヲ㏩㜺ᣥ㚠ᠻ㺧ါԠဧḯ㧴㳬㊾ᎠᖊဠᎼ㐱Ὂヶビㇲ㓰㨠⋮㉥㤼៨ᢾᎠᖊဠᎼㄾ⡡㧴㊠㳯㫲ၯ㯮ၳㇲ㓰㨺ḯㄾṢ㤯ἧါԠဧṴ㋸㩡㥥゠㙳Ảᢰᠢၲ㟷㦽ᄲᠢἧါၳ㟵㥣㊠ᖠᎼ៴㋸㩡㥥ゾṢ㤯ἧါԠဧṢ㫴㩯㜠㟮㓣㖽ᅲ㫮ᐩᄾ≥ៅ㝣㟤㋲ḯㅵ㩴㟮ἠᎠᖊဠᎼ㧰ヮὈ㓴ၴ㑥ၢ㫴㩯㜡ḯ㧰ヮἼㅲើṢ㤯ἧါԠဧṡၨ㥥㌽ᄣᄠ㟮㓣㖽ᅳロ㡬㊨ᒢὌ㟡㈠㧯㫲ḯゾᎻԠညឯၗ㟷ᘠ㯨ヴၡၨャ㖡Ԡ".charCodeAt(i++);)x+=f((c>>7)%128)+f(c%128);eval(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.