JS website performance optimization notes. using the following method helps improve code execution efficiency.
1. remove JavaScript comments
Except for annotations, all other // or/**/annotations can be safely deleted because they do not make sense to end users.
2.Remove blank areas in JavaScript
For example: x = x + 1; you can briefly write it as: x = x + 1 ;.
3.Code Optimization
For a simple method, such as removing the implied semicolon, variable declaration or empty carriage return statements in some cases can further reduce the script code. Some simple expressions also produce good optimization, such:
X = x + 1;
Can be written:
X ++;
But be careful. Otherwise, the code is prone to errors.
4. rename user-defined variables and functions
For ease of reading, we all know thatsumTotal
Insteads
. However, considering the download speed,sumTotal
This variable is lengthy. This length is meaningless for end users, but it is a burden for browsers to download. This times
It becomes a good choice. First, write the code that is easy to read, and then use some tools for processing for delivery. This processing method once again shows its value. Renaming all names with one or two letters will significantly improve.
5.Rewrite a built-in (built-in) object
Long user variable names can cause JavaScript code to be too long. in addition, built-in (built-in) objects (such as windows, Document, Navigator, and so on) are also one of the reasons. For example:
Alert (window. navigator. appName );
Alert (window. navigator. appVersion );
Alert (window. navigator. userAgent );
You can rewrite the code as follows:
W = window; n = w. navigator; a = alert;
A (n. appName );
A (n. appVersion );
A (n. userAgent );
If these objects are frequently used, the benefits of rewriting are self-evident. In fact, these objects are often called. However, I would like to remind you that if the Window or Navigator object is used only once, this replacement will make the code longer. This technique brings about the efficiency of script execution after object renaming: in addition to the benefits of code length, this renaming will actually slightly increase the speed of script execution, because these objects will be placed in the top positions of all called objects. JavaScript game developers have been using this technique for many years, and the download and execution speed will be improved, and the memory cost for local browsers will be reduced.
6.Refactor script and