Why assign Math and document to local vars?
adrianblynch opened this issue · 2 comments
adrianblynch commented
Not an issue as much as a question...
Here you are assigning Math
and document
to _math
and _document
respectively, why is that?
https://github.com/phoboslab/underrun/blob/master/source/game.js#L3-L4
Thanks.
phoboslab commented
The build.sh
concatenates all .js files and runs it through uglifyjs
. Assigning global objects to local names helps uglifyjs to squeeze out a few extra bytes in the minified file if these objects are used often.
E.g. consider:
for (var i = 0; i < Math.PI*2; i+=0.1) {
var x = Math.sin(r);
var y = Math.cos(r);
draw(x, y);
}
Will be minified to:
for(var a=Math,h=0;h<2*Math.PI;h+=.1){var t=Math.sin(r),M=Math.cos(r);draw(t,M)}
If we assign the global Math
to a local var:
var _math = Math;
for (var i = 0; i < _math.PI*2; i+=0.1) {
var x = _math.sin(r);
var y = _math.cos(r);
draw(x, y);
}
We will end up with:
for(var a=Math,v=0;v<2*a.PI;v+=.1){var d=a.sin(r),f=a.cos(r);draw(d,f)}
Maybe uglifyjs has a flag to do this automatically? I don't know. It worked out fine for my project, though.
adrianblynch commented
Thanks @phoboslab.
Really good game!