Search code examples
javascriptuglifyjsuglifier

Uglifying JavaScript - Further optimisation


This might seem like a stupid question but is there some reason why Uglifiers don't uglify references to external functions using a pointer?

An example:

A file I'm working on has multiple calls to _gaq.push(). I've squished this by adding to the top of the file var _g=_gaq.push();. I then update the 12 calls to it to use the _g pointer. This hasn't saved a huge amount (84 bytes) but every little helps right?

Am I missing something, is there a reason Uglifiers don't do this?


Solution

  • In short, because there is too much potential for the code to break.

    Assuming you mean var _g=_gaq.push;, then two possible causes of breakage spring to mind:

    1. You've changed the value of this inside push. If the functionality of push depends on that, then you just broke it.
    2. If the value of _gaq.push is changed after you assign it to _g then your code would continue to operate on the old value.