Alex Rivera | Logout

Does Google Closure Compiler ever decrease performance?

Asked 2011-11-09T13:45:01.323
30

I'm writing a Google Chrome extension. As the JavaScript files are loaded from disk, their size barely matters.

I've been using Google Closure Compiler anyway, because apparently it can make performance optimizations as well as reducing code size.

But I noticed this at the top of my output from Closure Compiler:

var i = true, m = null, r = false;

The point of this is obviously to reduce the filesize (all subsequent uses of true/null/false throughout the script can be replaced by single characters).

But surely there's a slight performance hit with that? It must be quicker to just read a literal true keyword than look up a variable by name and find its value is true...?

Is this performance hit worth worrying about? And is there anything else Google Closure Compiler does that might actually slow down execution?

Edit
Report

1 Answer

2

I think there will be a very slight performance penalty, but unlikely to matter much in newer, modern browsers.

Notice that the Closure Compiler's standard alias variables are all global variables. Which means that, in an old browser where the JavaScript engine takes linear time to navigate functional scopes (e.g. IE < 9), the deeper you are within nested function calls, the longer it takes to find that variable which holds "true" or "false" etc. Almost all modern JavaScript engines optimize global variable access so this penalty should no longer hold in many cases.

In addition, there really shouldn't be many places where you'd be seeing "true" or "false" or "null" directly in compiled code, except for assignments or arguments. For example: if (someFlag == true) ... is mostly just written if (someFlag) ... which is compiled by the compiler into a && .... You mostly only see them in assignments (someFlag = true;) and arguments (someFunc(true);), which really do not happen very frequently.

Conclusion is: although many people doubt the usefulness of the Closure Compiler's standard aliases (me included), you shouldn't expect any material performance hit. You also shouldn't expect any material benefits in gzipped sizes, though.

answered 2011-11-11T02:56:52.627

Your Answer