1. Respect for object ownership
? Do not add attributes for instances or prototypes;
? Do not add methods to instances or prototypes;
? Do not redefine a method that already exists.
the best way to do this is to never modify the objects that are not owned by you. The so-called owning object means that this object is created by you , such as a custom type or object literal that you create yourself. And the Array, document these are obviously not yours, they exist before your code executes.
2. Avoid global variables
Create at most one global variable so that other objects and functions exist. Take a look at the following example:
// two global amount--Avoid!! var name = "Nicholas"; function Sayname () {alert (name);}
This code contains two global values: the variable name and the function Sayname (). You can actually create an object that contains both, as shown in the following example:
// a global amount--recommended var MyApplication ="Nicholas"function() {alert (this. name);}};
3. Avoid comparisons with null
? If the value should be a reference type, use the instanceof operator to check its constructor;
? If the value should be a basic type, use typeof to check its type;
? If you want the object to contain a particular method name, use the typeof operator to ensure that the name method exists in the the elephant.
4. Using Constants
Although JavaScript does not have a formal concept of constants, it is useful. This idea of separating data from application logic can change data without risking the introduction of errors. take a look at the following example:
var Constants ="Invalid value!" "/errors/invalid.php"}; function Validate (value) {if (! = Constants.invalid_value_url;}}
5. Avoid global lookups
Perhaps the most important thing to optimize script performance is to pay attention to global lookups. Using global variables and functions is certainly more expensive than local, because it involves finding on the scope chain. Consider the following function:
UpdateUI () { var IMGs = document.getElementsByTagName ("img" ); for (var i=0, len=imgs.length; i < Len; I++) {imgs[ I].title = document.title + "image" + I;} var msg = document.getElementById ("msg" = "Update complete."
find. By creating a local variable that points to the Document object, you can improve the function's by restricting the global lookup once."
UpdateUI () { var doc = document; var IMGs = Doc.getelementsbytagname ("img" for (var i=0, len=imgs.length; i < Len; I++) {imgs[ I].title = doc.title + "image" + I;} var msg = Doc.getelementbyid ("msg" = "Update complete."
here, the Document object is first present in the local doc variable, and then the original document is replaced in the remaining code. compared with the original version, now the function is only a global lookup, certainly faster.
6, performance of other considerations
? native methods are faster -whenever possible, use native methods instead of using JavaScript to rewrite one. The native method is usedcompiled languages such as C + + are written out, so much faster than JavaScript. In JavaScript, the mosteasy to forget is the complex mathematical operations that can be found in the Math object, which are more than anything written in JavaScriptthe same method as sine, cosine is much faster.
? The switch statement is faster -if you have a series of complex if-else statements, you can convert to a single switch statement toto get faster code. You can also organize case statements in the most probable to most improbable order,steps to optimize the switch statement.
? bit operators are faster --when doing mathematical operations, bit operations are faster than any Boolean or arithmetic operations. Selectedreplacing arithmetic operations with bitwise operations in a selective way can greatly improve the performance of complex computations. such as modulo, logic and logic, or both can beto consider replacing with a bitwise operation.
JavaScript Performance Optimizations