Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does removing comments improve code performance? JavaScript

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.

like image 341
flavour404 Avatar asked Sep 08 '09 22:09

flavour404


2 Answers

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.

like image 64
Eric J. Avatar answered Oct 22 '22 04:10

Eric J.


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).

like image 21
SLaks Avatar answered Oct 22 '22 04:10

SLaks