1. Use global variables less
2. Try to use local variables to encapsulate global variables
Reason: As mentioned before, the active object is at the top of the stack, so its content is searched first. When we encapsulate the document into a local variable, the number of deep searches will be reduced and the performance will be improved.
3. When operating array lengths and object properties, try to use local variable encapsulation.
Reason: IE and Opera access arrays faster than object properties, while FF chrome safari is just the opposite, so taking these into account, we’d better encapsulate it.
4. Minimize the depth of object properties
5. Try to use local variables to encapsulate conditional items in the for loop
Reason: for example for(var i=0;i
6. Handle HTML collection objects carefully (such as childNodes getElementsByTagName and other objects that obtain DOM element collections). It is best to encapsulate them into arrays for operation.
Reason: It is still because operating DOM elements requires traversing the DOM document, but non-DOM elements do not need to be traversed, so please minimize the operations on the DOM and put the DOM collection into an array.
7. During the development process for Safari browser, please try to use “.” to obtain object properties instead of “”.
8. It is recommended to use if-else when there are less than two judgments, and use switch when there are more than three judgments. When it exceeds 10 times, use an array or json object to search by index (this mode is relatively simple)
9. If there is no difference in the order of looping the array from low to high or from high to low, then it is better to loop from high to low. For example, if we loop the contents of an array:
var arr=[1,2,3,4,5]; var i=0; while(i
10. Use for-in loops as little as possible and transform them into while or for loops as much as possible.
11. When dealing with large arrays, follow the duff strategy.
duff strategy: Split the large array into a group of 8. There is no need to loop the operation of the 8 arrays as one unit. Instead, you are not afraid of the tedious writing of 8 lines of code to process the array elements. This will improve the performance of large array operations.
12. When using a function to process each element of a large array, try to use a timer to suspend each operation. It is more reasonable to set the time to 50-100ms
13. Pay attention to the blocking of window onload when using iframe
Reason: The loading of iframe will block the window onload, resulting in some codes loaded in the window unload event may not be executed when the user closes the page, so we need to dynamically convert it to an iframe when the window onload time occurs. Set the src attribute.
PS: Don’t use iframe if you can.
14. Optimization of CSS selectors
Reason: CSS selectors are read from right to left, so when writing, the rules on the right should be as detailed as possible, and sub-selectors should be used as little as possible to comply with descendant selectors, label selectors.
The above is the entire content of this article. I hope it will be helpful to everyone's study and I hope you will support me a lot.