views:

304

answers:

6

Hi,

Does removing comments from JavaScript code improve performance?

I realize that this is not great programing practice as comments form an intrinsic part of development. I am just interested to know if they do in fact add some overhead during compilation.

Thanks, Ron.

+8  A: 

Whether your compiling or interpreting your JavaScript, the compiler/interpreter needs to look at the line, decide it's a comment, and move on (or look at a region of the line). For web applications, the comment lines also need to be downloaded.

So yes, there is some overhead.

However, I doubt you could find a real-world scenario where that difference matters.

If you are compiling your code, the overhead is only during the compile run, not during subsequent execution.

Eric J.
+5  A: 

Removing comments will make the Javascript file smaller and easier to download.

Other than that, it will not affect noticably performance at all.

If you're concerned about bandwidth and want to make the file smaller, the best thing to do is to the file through JSMin or a similar tool before deploying it to your production web site. (Make SURE to keep the original file, though).

SLaks
A: 

Another issue is that comments of the sort "This code is crap but we must meed the deadline" may not look as good in customer's browser.

quant_dev
Not an answer to the question.
Snarfblam
I found it funny.
flavour404
A: 

I'm not sure about runtime speed, but removing comments will decrease download size, which is just as important.

You should always have comments in the code you work on, and use a minifier to strip them out for deployment - YUI Compressor is a good one.

Chi
It's not necessarily true that you should always strip them out. That's a trade-off between time (download time for your end users) and a little bandwidth cost vs. complexity of introducing this to the build/deploy process.
Eric J.
+1  A: 

It would make no noticeable difference to the execution of the JavaScript.

What it does make a difference to is the size of the JavaScript files being downloaded to client browsers. If you have lots of comments, it can significantly increase the size of the JavaScript files. This can be even more so with whitespace characters used for layout.

The usual approach is to "minify" .js files before deployment to remove comments and whitespace characters. Minifiers can also rename variables to shorter names to save extra space. They usually make the original JavaScript unreadable to the human eye though, so it is best to make sure you keep a copy of the un-minified file for development.

adrianbanks
+1  A: 

Performance lag while browser interprets code? No significant difference. But it does add to bytesize which makes is longer to download.

But that's no reason to omit comments. Keep your development codebase commented. Use javascript compressors prior to release.

Also, during the release, try to bunch up your entire javascript codebase for a page inside a single file so as to minimize HTTP requests. HTTP requests bear a significant performance penalty.

gAMBOOKa
Good point, http requests. I'm trying desperately to speed up this site which uses multiple .js files. I'll keep this in mind.
flavour404
Get Yahoo Slow plugin and Google's PageSpeed.
gAMBOOKa