Replace variable names
Remove all line breaks
The next most important (and simple) thing you can do to decrease the size of the script file is to remove
all line breaks. As long as you program appropriately by including a semicolon at the end of each line,
the line breaks are of no consequence.
increase the readability of your code to prying eyes. Removing them is a fast, easy way to make it more
difficult for anyone to reverse engineer your code.
Replace variable names
This is the toughest optimization method to implement. Replacing variable names usually can’t be done
by hand because the process is not a standard find-and-replace text operation.
The basic idea is that any variable name (or private property of an object) should be replaced with a non-
sense variable name that has no intuitive meaning when read in the code. After all, the name of the vari-
able doesn’t matter to the interpreter, only to the developer who wants his or her code to make sense.
When it comes time to deploy the script, however, eliminate those descriptive variable names with sim-
pler, shorter ones:
The first line of code above is the original; the second line has the argument names replaced. By doing
this, the byte count was reduced by 16. Just imagine the savings if all the variable names in your entire
script are replaced with two-character names.
The ECMAScript cruncher
Following the four steps listed previously can be difficult. To help with the process, you can use an
Use extreme caution when trying to rename variables on your own. Using a standard
Find and Replace method in a text editor is not recommended, as the editor can’t tell
the difference between a variable name and any other text that matches the given
pattern. For example, you may have a variable named
(perhaps a Boolean indicat-
ing if a value is valid or not). If you try to replace
with another value, you also
at the end of
, thus rendering your entire script useless.
If for some reason removing line breaks is not an option, then — at the very least —
make sure that they are Unix format instead of Windows format. Windows uses two
characters on a line break (carriage return and line feed, ASCII codes 13 and 10
respectively); Unix uses only one. So, translating the line breaks into Unix format
from Windows still yields some byte savings.
22_579088 ch19.qxd 3/28/05 11:43 AM Page 575