Byte-saving techniques in JavaScript
github.com
github.com
Since version 1.5, JavaScript accepts Unicode characters as identifiers. Shouldn't this be "character-saving"?
if(condition)
//{ //save a byte!
action();
//} // save a byte!
as if leaving out the braces will actually make your code run faster, or make a real difference after minification. I could easily see that sort of person thinking this article is like an elite JavaScript style guide.I'm currently writing a mobile web app that needs to work over GPRS connections with 30kb/s bandwidth and up to 1s latency. In these cases, shaving off a few bytes to get a page in 1 fewer TCP/IP packets really does make a difference.
If you are writing mobile apps you should check out XUI instead of jQuery. jQuery (even jQ Mobile) is super heavy. XUI is jQ like, but it is by the Nitobi guys (of PhoneGap fame) and they really get mobile and did it right.
function P(a){for(p=[],i=Math.pow(2,l=a.length);i;)for(b=i.toString(2),j=k=b.length,p[--i]=[];j;)if(b[--j]==1)p[i].push(a[j+l-k]);return p}
I know that there are some big gotchas here, such as b and p being declared globally due to lack of var...but I gave up trying to gain an extra 4 bytes for function(a,b,p). Also the subset arrays are in reverse order. Math.pow(2,l=a.length)
1<<(l=a.length)
are the same, as are these: if(b[--j]==1)
~-b[--j]|| rand10=Math.floor(Math.random())*10 // before
rand10=0|Math.random()*10 // after
The first is not random, it will always return zero (this error is very common). The second happens to be ok because * binds tighter than |. However, this is order-dependent.This style of code, while not for everyone, exploits the "happens to be okay" operator precedence. For this, Mozilla's MDC is a great resource:
https://developer.mozilla.org/en/JavaScript/Reference/Operat...
I tried to write most of my JS for 10k 'pre-minified', using tricks akin to (but not as awesome as) these. Echoing other commenters, it was largely as an intellectual exercise (to see if I could meet the 10k limit by hand) but it did make me think of a few things in a different light, and some of the 'optimizations' are things that minifiers wouldn't have done for me.
It's also a good way to get under the skin of a language, get a feel for the nuances of how the runtime behaves, and give you hints for other (more generally useful) optimisations.
At the smallest end you really have to work with what you're given and triage fast, whereas with JS1K you see people writing decompressors _in_ their code to get around JavaScript's occasional verbosity.
Math.floor(someNumber) // 2.6 becomes 2
someNumber | 0 // 2.6 becomes 2
Not only because it saves characters, but because it might actually be used in the real world (Whereas most of the ones in the article should really be left to a minifier) (Math.pow(2, 31) - 0.1) // base number
2147483647.9
~~(Math.pow(2, 31) - 0.1) // works!
2147483647
(Math.pow(2, 31) + 0.1) // base number
2147483648.1
~~(Math.pow(2, 31) + 0.1) // doesn't work!
-2147483648