How to quickly get started with VUE3.0: Enter learning.
I have been looking for tips on jQuery performance optimization to make my bloated dynamic web application lighter. After searching many articles, I decided to list some of the best and most commonly used suggestions for optimizing performance. [Recommended learning: jQuery video tutorial]
1. Selector performance optimization suggestions
1. Always inherit from the #id
selector
. This is a golden rule for jQuery selectors. The fastest way to select an element in jQuery is to select it by ID.
2. Use tag in front of class
The second fastest selector in jQuery is the tag selector (such as $('head')), because it comes directly from the native Javascript method getElementByTagName(). So it's best to always use tags to modify classes (and don't forget the nearest ID).
The class selector in jQuery is the slowest, because it will traverse all DOM nodes in IE browser. Try to avoid using class selectors. Don't use tags to modify IDs either.
3. Use subqueries
to cache parent objects for future use
4. Optimize selectors to adapt to Sizzle's "right-to-left" model
Since version 1.3, jQuery uses the Sizzle library, which is the same as previous versions in the selector engine. The expressions are very different. It replaces the "right to left" model with a "left to right" model.
5.
It is indeed faster to use find() instead of using the context to find the find() function. But if a page has many DOM nodes, it may take more time to search back and forth:
6. Use powerful chain operations. Using
jQuery's chain operations is more effective than caching selectors.
7. Write your own selector
if You often use selectors in your code, so extend jQuery's $.expr[':'] object to write your own selectors.
2. Suggestions for optimizing DOM operations
8. Cache jQuery objects and
cache elements you often use.
9. When inserting into the DOM, encapsulate all elements into one element.
The basic idea here is to create what you really want in memory. , and then update the DOM. This is not a jQuery best practice, but is required for valid JavaScript operations. Direct DOM operations are very slow. Direct DOM operations are very slow. Change the HTML structure as little as possible.
10. Although jQuery does not throw exceptions, developers should also check objects.
Although jQuery does not throw a large number of exceptions to users, developers should not rely on this. jQuery usually executes a bunch of useless functions before determining whether an object exists. So before making a series of references to an object, you should first check whether the object exists.
11. Use direct functions instead of equivalent functions.
For better performance, you should use direct functions like .ajax() instead of .ajax() instead of .ajax(). Do not use .get(),.getJSON(),.getJSON(),.getJSON(),.post(), because the latter ones will call $.ajax().
12. Cache jQuery results for later use.
You will often get a javascript application object - you can use App. to save the objects you often select for future use.
13. Use jQuery's internal function data() to store state.
Don’t forget to use the .data() function to store information.
14. Use the jQuery utility function
. Don’t forget the simple and practical jQuery utility function. My favorites are $.isFunction(), isA rray() and isArray() and isArray() and .each().
15. Add the "JS" class to the HTML block
. After jQuery is loaded, first add a class called "JS" to the HTML $('HTML').addClass('JS'); only when the user enables JavaScript, You can add CSS styles.
3. Suggestions on optimizing event performance
16. Defer to (window). Load
sometimes uses (window).load. Sometimes it uses (window).load. Sometimes it uses (window).load() than $(document).ready. () is faster because the latter does not wait until all DOM elements have been downloaded. You should test it before using it.
17. Use Event Delegation.
When you have many nodes in a container and you want to bind an event to all nodes, delegation is very suitable for such application scenarios. Using Delegation, we only need to bind the event at the parent and then see which child node (target node) triggered the event. This becomes very convenient when you have a table with a lot of data and you want to set events on the td node.
18. Use the abbreviation of ready event.
If you want to compress the js plug-in and save every byte, you should avoid using $(document).onready()
4. Test jQuery
19. jQuery unit test
The best way to test JavaScript code is to use people Come and test. But you can use some automated tools such as Selenium, Funcunit, QUit, QMock to test your code (especially plug-ins). I want to discuss this topic in another topic because there is so much to say.
20. Standardize your jQuery code.
Standardize your code often, see which query is slower, and replace it. You can use the Firebug console. You can also use jQuery's shortcut functions to make testing easier.
5. Other common jQuery performance optimization suggestions
21. Use the latest version of jQuery.
The latest version is often the best. After changing versions, don't forget to test your code. Sometimes it's not completely backwards compatible.
22. Using HMTL5
The new HTML5 standard brings a lighter DOM structure. A lighter structure means fewer traversals are required when using jQuery, and better loading performance. So please use HTML5 if possible.
23. If you want to add styles to more than 15 elements,
the best way to add style tags directly to DOM elements is to use jQuey’s css() function to style a few elements. However, when adding styles to more than 15 elements, it is more effective to add style tags directly to the DOM. This method avoids using hard code in the code.
24. Avoid loading redundant code.
It is a good idea to put Javascript code in different files and load them only when needed. This way you won't load unnecessary code and selectors. It is also easy to manage code.
25. Compress into one main JS file to keep the number of downloads to a minimum.
When you have determined which files should be loaded, package them into one file. Use some open source tools to do it for you automatically, such as using Minify (integrated with your back-end code) or using online tools such as JSCompressor, YUI Compressor or Dean Edwards JS packer to compress files for you. My favorite is JSCompressor.
26. It’s great to use native Javascript when necessary.
Using jQuery is a great thing, but don’t forget that it is also a framework for Javascript. So you can use native Javascript functions when necessary in jQuery code, which can achieve better performance.
27. Load jQuery framework from Google
When your application is officially launched, please load jQuery from Google CDN, because users can get the code from the nearest place. This way you can reduce server requests, and if the user browses another website that also uses Google CDN's jQuery, the browser will immediately call out the jQuery code from the cache.
28. Slowly loading content can not only improve loading speed, but also improve SEO optimization. Use Ajax to load your website, which can save server-side loading time. You can start with a common sidebar widget.
[Recommended learning: jQuery video, web front-end development]
The above is how to optimize jQuery performance? For details on the summary of optimization methods, please pay attention to other related articles on the PHP Chinese website for more information!