Suggestion: Alias native methods

18 views
Skip to first unread message

cjavapro

unread,
Feb 7, 2011, 6:19:02 PM2/7/11
to Closure Compiler Discuss
If I am in the wrong place for a feature suggestion then please let me
know.

If your compiler creates the lines

var googleRandomSuff_setTimeout = setTimeout
var googleRandomSuff_clearTimeout = clearTimeout

at the beginning of a advanced compilable file and then renames
references of setTimeout to googleRandomSuff_setTimeout and
clearTimeout to googleRandomSuff_clearTimeout .

Then your existing functionality takes over and renames them to short
names. In the event that neither of those are used, your compiler will
remove them with its existing features. In the event it is used once,
your compiler will undo what I mentioned above using nothing but its
already existing features.

Then for every additional call to either of those, you will save an
average of 9 bytes.

That may not seem so cool, but you can do this for window, Date,
parseInt, parseFloat and many others.

In fact, even the scope problems that might have been created would be
eliminated by automatic features as this

setTimeout(...)
var setTimeout = function(){...}
setTimeout(...)

would change to this

var googleRandomSuff_setTimeout = setTimeout
googleRandomSuff_setTimeout(...)
var googleRandomSuff_setTimeout = function(){...}
googleRandomSuff_setTimeout (...)

which does the exact same thing.

Nick Santos

unread,
Feb 7, 2011, 6:30:24 PM2/7/11
to closure-comp...@googlegroups.com
This is available from the compiler's java API. It's called aliasExternals.

It isn't generally useful because gzip does better, and you really
should just serve your JS gzipped.

Reply all
Reply to author
Forward
0 new messages