Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do comments make the code run slower?

Tags:

comments

php

I heard that a heavily commented script runs slightly slower than a non-commented one. Is it true?

Did anyone test this? (like how much slower is it in percentages)

like image 847
Alex Avatar asked Mar 24 '11 17:03

Alex


People also ask

Do comments affect code performance?

The biggest effect that comments have is to bloat the file size and thereby slow down the download of the script.

Do comments slow down HTML?

Since html minification on dynamically generated pages isn't that common, comments in html slow you down a bit, but they typically are so few that it doesn't matter in practice either.

Do comments slow down code JS?

Whitespaces and comments increase the size of the JavaScript file, which slows down the actual downloading of the file from the server - minification is the process of stripping unnecessary characters from a JavaScript file to make it smaller and easier to download.

Do HTML comments affect performance?

What are the negatives of HTML comments? worsens the UX - even not visible, the HTML comments are part of the DOM tree and increase the number of DOM elements. This, in turn, affects the quickness and responsiveness, also leads to slower CSS selectors and DOM manipulation.


2 Answers

Commenting will not affect the script execution time in normal case. But the number of lines you write in your code affect the parser to read and buffer it considerably. If you can execute certain things in 20 lines, you try to write the same thing in 1000 lines, the performance might be affecting if its part of an application which executes sequentially. Even if few lines or lot of lines the dependencies are important. If you are using a library which is heavily depending on some applications, obviously the loading time, parsing time and compile and execution time etc will increase. In any case the commenting will not affect considerably, but a few microseconds will not cost you much. So go ahead and comment your code and make it readable by co-developers.

like image 50
Architect Avatar answered Oct 16 '22 03:10

Architect


I can tell you that 99.99% of the time spent parsing the following file:

<?php /* A comment */ ?> 

Is spent on opening the file, reading its contents, and closing the file. If you copied and pasted that comment onto 10,000 lines, it'll make no difference.

like image 28
BoltClock Avatar answered Oct 16 '22 05:10

BoltClock