Skip to content

Instantly share code, notes, and snippets.

@JavaScript-Packer
Created April 22, 2015 08:33
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 JavaScript-Packer/46c8503f2346ca585aa2 to your computer and use it in GitHub Desktop.
Save JavaScript-Packer/46c8503f2346ca585aa2 to your computer and use it in GitHub Desktop.
JavaScript Golfer Tool
JAVASCRIPT GOLFING TOOL DEMO - www.scriptcompress.com/golf.htm
One technique I use often is taking something like x=x.split(" ").join("");y=y.split(" ").join(""); that I may use often in a script and make it a bit smaller (or part of obfuscating) throughout the script. I will set it up like x=x[S='split'](" ")[J='join']("");y=y[S](" ")[J]("");
Take a script like:
var roll={d4:function(){return Math.floor(4*Math.random())+1},d6:function(){return Math.floor(6*Math.random())+1},d8:function(){return Math.floor(8*Math.random())+1},d10:function(){return Math.floor(10*Math.random())+1},d12:function(){return Math.floor(12*Math.random())+1},d20:function(){return Math.floor(20*Math.random())+1}};alert("rolling D4 : "+roll.d4());alert("rolling D20 : "+roll.d20());
Can be rewritten as:
var roll={d4:function(){return Math[F='floor'](4*Math[R='random']())+1},d6:function(){return Math[F](6*Math[R]())+1},d8:function(){return Math[F](8*Math[R]())+1},d10:function(){return Math[F](10*Math[R]())+1},d12:function(){return Math[F](12*Math[R]())+1},d20:function(){return Math[F](20*Math[R]())+1}};alert("rolling D4 : "+roll.d4());alert("rolling D20 : "+roll.d20());
We just saved 35 bytes on a small scale.
To get the value from a textareaa with id="whak", we normally have a long call for it like:
document.getElementById("whak").value
and we may need to call it many times. This can inflate our script fast!
If we set some variables like:
var W = document, H = "getElementById", A = "whak", K = "value";
now we can use:
W[H](A)[K]
instead of document.getElementById("whak").value all the time (saving 28 bytes each time eventually).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment