Use the best Selector
Commonly used selectors include the following:
1. ID selector $ ("# id ")
2. Tag selector $ ("td ")
3. class selector $ (". target ")
4. Attribute selector $ ("td [target = 'target']")
5. pseudo class selector $ ("td: hidden ")
Conclusion
1. The native method is the fastest method. If possible, try to use the native method.
2. Performance sequence: ID selector> tag selector> class selector> attribute selector> pseudo class selector
3. ID (getElementById), the tag selector (getElementsByTagName) all have native method correspondence, so very soon; class selector in addition to the IE5-IE8 of mainstream browsers almost all have native method (getElementsByClassName ), to ensure IE6, 7, and 8 performance, avoid using global class selectors;
4. Attribute and pseudo-class selector are very slow. If not necessary, use pseudo-class selector and attribute selector as few as possible.
Best practices
1. Set IDs for the elements and root elements that are most frequently operated in the module, and then cache them;
2. Search for elements without IDs. Use the ancestor element id with the shortest path as the default search range and use the pure class selector or tag selector;
3. Avoid complex selector whenever possible
Avoid performing global search
=>
1 |
$("#container").find(".bizHelp"); |
Ensure that the query path is the shortest and the performance is the best. Refer to the first article;
Avoid null results
For the selection result with a quantity of 0, JQuery performs the default action and does not report an error, which affects the program performance.
1 |
var blank=$(".blank");//length=0 |
=>
1 |
blank.length && blank.slideUp(); |
Avoid using anonymous Functions
Using a large number of anonymous functions increases the difficulty of debugging, maintaining programs, and performing performance tests through third-party software. Therefore, you should avoid using a large number of anonymous functions as much as possible.
>>>
1 |
var clickHandler=function(){ |
4 |
obj.click(clickHandler) |
Large loops adopt a more efficient Traversal method
JQuery provides $. fn. each () and $. the each () method can be used to traverse the set. In addition, the JS native for loop and while can be used to achieve iteration:
1 |
var list=ul.find("li"),e; |
A:
B:
C:
1 |
$.each(list,function(){ |
Conclusion
In general, A> C> B
Best practices
Pursuing ultimate performance, using native methods;
If the number of loops is small, we recommend that you use $. each (), which is relatively stable;
Use native attributes first
Many common attributes, such as id and name, are implemented by the browser. In JQuery, we sometimes use $ (this ). attr ("id") method to obtain the id, the efficiency of this method is very slow compared to the efficiency of native attribute acquisition.
1 |
$.each(list,function(){ |
3 |
var id=$(this).attr("id"); |
Conclusion
Using Native APIs can greatly improve performance
Best practices
For common attributes such as id, native attributes should not be obtained through attr;
Use event Delegate
To add a click event to all elements in a list, the traditional method is to get the JQuery object: $ ("li") in this list, and then add the click event:
1 |
$("li").click(function(){}) |
This method may cause serious performance problems when the number of lists is large and deserves attention.
JQuery's event delegation mechanism can be used to greatly reduce the consumption of event listening and memory.
A:
1 |
var list=$("li");//length>1 list.click(function(){ }) |
B:
1 |
$("ul").delegate("li","click",function(){}) |
Conclusion
The delegated performance advantage is very absolute;
Best practices
If you want to add events to more than two elements of the same type at the same time, use solution B to replace.
Use $. data instead of $. fn. data
View Source code printing?
1 |
$(elem).data(key,value); $.data(elem,key,value); |
The latter is nearly 10 times faster than the former