;div>
});
7. Handle loops correctly
Looping is a time-consuming operation. If you can use a selector to directly select elements, do not use loops to traverse the elements one by one.
Javascript’s native methods for and while are faster than jQuery’s each(). Therefore, native methods should be used first.
8. Reduce the generation of JQuery objects
Generating a Query object will generate corresponding properties and methods, which takes up more resources. So try to reduce the generation of jQuery objects.
9. Scope of variables
When a variable is not needed in multiple function calls, the variable should be placed within the function to reduce the time of searching for the code when the code is executed.
10. Defer certain functions to $(window).load execution
$(document).ready is really easy to use, but it can be executed before other elements are downloaded when the page is rendered.
11. Merging of scripts
Scripts are loaded one by one, reducing the number of scripts can also improve efficiency.
12. Element encapsulation
When inserting content into a node, you can encapsulate the content first and then insert it.
var content = "";
$(" #head").html(content);
The other is to compress js files.
As jQuery continues to be used, more and more optimization methods will be discovered.
Related articles
See more- An in-depth analysis of the Bootstrap list group component
- Detailed explanation of JavaScript function currying
- Complete example of JS password generation and strength detection (with demo source code download)
- Angularjs integrates WeChat UI (weui)
- How to quickly switch between Traditional Chinese and Simplified Chinese with JavaScript and the trick for websites to support switching between Simplified and Traditional Chinese_javascript skills