When you do something like this, you have to make some heart-breaking feature cuts though. Of course, physics and animation was the first thing to go. Then I slowly removed some of randomization and visual error checking rules that I had in place. This got it down for a few kb, but still needed some major reworking. I ended up restarting from scratch and thought through the best way to put it all together before getting a few other eyes on it to see how else I could squeeze it down to a slim 1K.
Some of this may be obvious, but here are some tips for slimming down your code for JS1K:
- There are a lot of js minifyers out there. The one that worked best for me was google’s closure compiler
- When manually shortening variables and function names, comment the hell out of it.
- Processing proved again to be a great environment for prototyping my ideas. I would suggest doing that first to see if there’s any math and basic drawing that needs to be figured out first, if possible.
- Math and any common Math functions can be shortened at the beginning of your code as
var M=Math, N=M.random, C=M.cos, S=M.sin, Pw=M.pow // and so on
- Instead of Math.floor, you can use ~~
is the same as
- Ternary style if/else statements to define variables saves you a good amount of bytes. Use whenever you can.
- Looking at what other kinds of optimisations have been done for JS1K in the past was also helpful, including blog entries by Román Cortés, Skilldrick, and nooshu.
You can view my JS1K entry here.