Page Nav

HIDE

Pages

Classic Header

{fbt_classic_header}

Breaking News:

latest

A Christmas Tree in 1k of Javascript

JS1k is a really nice contest for coding tiny 1 kb Javascript programs and try to do something nice in that few available space. ...



JS1k is a really nice contest for coding tiny 1 kb Javascript programs and try to do something nice in that few available space.




















Finally, some size optimization tricks

Used several tricks to make the code fit in 1 kb. The most of them are the usual tricks for reducing Javascript code size, like renaming variables to single character names, removing white spaces, unnecesary semi-colons and so on.

It is also very common to assign functions you use more than one time to variables to avoid the repetition. For example, if I do r=Math.random;, I can call r() each time I need a random number.

Used some few math tricks, like using cos(angle+11) to approximate sin(angle) and modular arithmetic.

Finally, used some other tricks like reusing functions. A function does different things depending on if it received parameters or not. This saves some few bytes from writing again “function”.

Writing code for limited size competitions is mostly like a puzzle or logical game. In the most of the cases there is not a common rule to do it, but just thinking and trying different ways to achieve the same goal in less space. It is usually possible to fit 5 or 6 kb of normal code in 1 kb if you try it and don’t give up.

No comments