Take the 2-minute tour ×

My understanding of JavaScript “compilation” is that it condenses and minifies your code to ultimately save bytes.

Does either condensing or minification make JavaScript run any faster?

Take the following examples for consideration:

var abcdefghijklmnopqrstuvwxyz = 1;
// vs.
var a=1;
var b = function() {
    // Here is a comment
    // And another
                                                                                                                        // White space
    return true;

// vs.

var b=function(){return true}

I ran these examples through jsPerf with little or no difference.

Can compilation of JavaScript make it any faster or slower, in addition to saving bytes?

share|improve this question
What you are referring to is not in any way compilation. I'd suggests that you edit your question. –  Jan Hančič Nov 20 '12 at 13:06
So if I run my JS through google closure compiler, its in no way compiled? developers.google.com/closure/compiler –  Blowsie Nov 20 '12 at 13:08
javascript is javascript. The js engine may optionally compile javascript, but it in itself is not compiled in any way. Any performance benefit is either caused by code-rewrite or is negligible (as in your example.) –  Zirak Nov 20 '12 at 13:09
@Zirak You can compile javascript to javascript. See the answer. –  Jan Dvorak Nov 20 '12 at 13:10
@JanDvorak And if I start calling my dog a lemon, will she turn into a lemon? –  Zirak Nov 20 '12 at 13:14

2 Answers 2

up vote 12 down vote accepted

Yes, compilation in the sense of the transforms applied by something like the Google Closure Compiler can make your script run faster. Consider this very simple example:

var x = ["hello", "james"].join(" ");

That compiles to:

var x="hello james";

Which is both less code and quicker to run. Obviously that's a silly example. I would hope you would write the compiled version yourself. However, it does demonstrate that Closure is capable of giving performance improvements as well as just file size improvements.

From the Closure docs (emphasis added):



For an example of the Closure compiler actually increasing the size of a JavaScript file in an attempt to offer performance improvements, see my answer to this question.

share|improve this answer
I guess uncompiled javascript also has potential to use more memory vs compiled javascript? –  Blowsie Nov 20 '12 at 13:12
@Blowsie - That's probably less likely, since the things you store in memory are unlikely to change as a result of the compilation (and things stored unnecessarily will be garbage collected anyway). But it is probably possible, yes. –  James Allardice Nov 20 '12 at 13:15
So in fact it is not a compiler but a tool which refactors for performance. –  Octavian Damiean Nov 20 '12 at 13:19
@OctavianDamiean - Pretty much, yes. Although Google do call it "a true compiler for JavaScript" (although according to Wikipedia, this violates the definition of "compiler"). –  James Allardice Nov 20 '12 at 13:20
@Blowsie Actually, the compiled javascript might take less memory. Google Closure Compiler often inlines functions, which means there is less stack used, thus less memory used. –  Florian Margaine Nov 20 '12 at 13:24

Minified vs un-minified shouldn't make any difference in terms of execution speed. The only difference could be that minified version would be faster to parse, but even that when you have a very large file (you won't find any differences with the test you ran, it's just to small).

edit: the first statement I've made is valid if you are doing just the basic "minification". If you are using Closure compiler like James showed, then there could be some differences if the Clousure tools would optimize your code ...

share|improve this answer

Your Answer


