Main Page

Remove all comments

takes for the script to run. The key factor in decreasing download time is the number of bytes that a
script contains.
The key number to remember is 1160, which is the number of bytes that fit into a single TCP-IP packet.
It’s best to try to keep each JavaScript file to 1160 bytes or less for optimal download time.
Every character in a JavaScript file is a byte. Thus, every extra character (whether it be a variable name,
function name, or comment) counts against the download speed. Before deploying any JavaScript code,
the download time should be optimized as much as possible. Here are a handful of ways that you can
decrease the overall number of bytes in a script.
Remove all comments
This should be a no-brainer, but many developers forget this because, once again, compilers have tradi-
tionally handled this.
Any comments in a script should be removed prior to deployment. Comments are important while you
are developing so that all team members can understand the source code. However, when it comes time
for deployment, those comments are slowing down your JavaScript code dramatically.
Removing comments is the easiest way to cut down the number of bytes in a JavaScript file. Even if
you don’t follow any of the other suggestions I give, this alone can provide dramatic decreases in overall
file size.
Remove tabs and spaces
Most good developers indent their code regularly in order to increase readability. This is good practice,
but the browser doesn’t need all those extra tabs and spaces; they must go. And don’t forget about the
spaces between function arguments, assignments, and comparisons: say goodbye to those as well.
Consider the following two lines:
function doSomething ( arg1, arg2, arg3 ) { alert(arg1 + arg2 + arg3); }
function doSomething(arg1,arg2,arg3){alert(arg1+arg2+arg3);}
To a JavaScript interpreter, these two lines are exactly identical, although the first line contains 12 more
bytes than the second. Removing the extra tabs and spaces between arguments, parentheses, and other
language delimiters helps to decrease the overall file size and, in turn, decreases the download time.
Using semicolons at the end of each line helps to preserve the syntactical meaning
of your code when extra tabs and spaces are removed.
It may be legally necessary to leave in a copyright notice or other such comment in
your file for deployment. If this is the case, ensure that all other comments are
removed and the legal comments are as short as possible.
Chapter 19
22_579088 ch19.qxd 3/28/05 11:43 AM Page 574

JavaScript EditorFree JavaScript Editor     Ajax Editor