First of all, keep in mind that jQuery is javascript. This means we should adopt the same coding conventions, style guides and best practices.
First of all, if you are a javascript newbie, I recommend you read "24 JavaScript Best Practices for Beginners". This is a high-quality javascript tutorial. It is best to read it before getting into jQuery.
When you are ready to use jQuery, I strongly recommend that you follow these guidelines:
1. Cache variables
DOM traversal is expensive, so try to cache reused elements.
2. Avoid global variables
jQuery As with javascript, in general it's best to make sure your variables are within function scope.
3. Use Hungarian nomenclature
Add $ prefix before variables to easily identify jQuery objects.
4. Use var chain (single var mode)
To combine multiple var statements into one statement, I recommend putting unassigned variables at the end.
5. Please use 'on'
In the new version of jQuery, the shorter on("click") is used to replace functions like click(). In previous versions on() was bind(). Since jQuery 1.7, on() is the preferred method for attaching event handlers. However, for consistency's sake, you can simply use the on() method all together.
$first.hover(function(){
$first.css('border','1px solid red');
})
$first.on('hover',function(){
$first.css('border','1px solid red');
})
6. Simplify javascript
In general, it’s best to combine functions whenever possible.
7. Chain operation
It is very easy to implement method chaining operations in jQuery. Take advantage of this below.
8. Maintain code readability
Along with streamlining the code and using chaining, the code may become difficult to read. Adding pinches and line breaks can work wonders.
Short-circuit evaluation is an expression that is evaluated from left to right, using the && (logical AND) or || (logical OR) operators.
10. Select shortcut
One way to streamline your code is to take advantage of coding shortcuts.
[code]
//Oops
if(collection.length > 0){..}
11. Separate elements during heavy operations
If you plan to do a lot of operations on DOM elements (setting multiple attributes or css styles in succession), it is recommended to detach the elements first and then add them.
$element=$containerLi.first();
//... Many complex operations
$element=$containerLi.first().detach();
//... Many complex operations
$container.append($element);
12. Memorize skills
You may be inexperienced with using methods in jQuery, be sure to check out the documentation, there may be a better or faster way to use it.
13. Parent elements cached using subqueries
As mentioned earlier, DOM traversal is an expensive operation. A typical approach is to cache parent elements and reuse these cached elements when selecting child elements.
Putting the universal selector into a descendant selector has terrible performance.
15. Avoid implicit universal selectors
Universal selectors are sometimes implicit and difficult to find.
16. Optimization selector
For example, the id selector should be unique, so there is no need to add additional selectors.
17. Avoid multiple ID selectors
It is emphasized that the ID selector should be unique, there is no need to add additional selectors, and there is no need for multiple descendant ID selectors.
18. Stick to the latest version
New versions are usually better: more lightweight and more efficient. Obviously, you need to consider the compatibility of the code you want to support. For example, version 2.0 does not support Internet Explorer 6/7/8.
19. Abandon deprecated methods
It is very important to pay attention to deprecated methods with each new version and try to avoid using these methods.
20. Utilize CDN
Google’s CND ensures that the cache closest to the user is selected and responds quickly. (Please search the address yourself when using Google CND. The address here cannot be used. We recommend the CDN provided by jquery official website).
21. Combine jQuery and javascript native code when necessary
As mentioned above, jQuery is javascript, which means that anything you can do with jQuery can also be done with native code. Native code (or vanilla) may be less readable and maintainable than jQuery, and the code is longer. But it also means more efficient (usually the closer to the underlying code the less readable the higher the performance, for example: assembly, which of course requires more powerful people). Keep in mind that no framework can be smaller, lighter, and more efficient than native code (note: the test link is no longer valid, you can search for the test code online).
Given the performance differences between vanilla and jQuery, I highly recommend taking the best of both and using (if possible) native code equivalent to jQuery.
22. Final advice
Finally, I recorded this article with the purpose of improving the performance of jQuery and some other good suggestions. If you want to delve deeper into this topic you will find a lot of fun. Remember, jQuery is not required, just an option. Think about why you want to use it. DOM manipulation? ajax? stencil? css animation? Or a selector engine? Perhaps a javascript microframework or a custom version of jQuery would be a better choice.