Performance Optimization of common JQuery Functions

Source: Internet
Author: User

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  $("div.bizHelp");

 

=>

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.slideUp();

 

=>

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 obj.click(function(){  
2   //do something...  
3 })

 

>>>

1 var clickHandler=function(){  
2   //do something...  
3 }  
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:

1  var i=list.length;  
2         while(i--){  
3             e=$(list[i])  
4         }

 

B:

1   list.each(function(){  
2           e=$(this);  
3         });

 

C:

1  $.each(list,function(){  
2           e=$(this);  
3         });

 

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(){   
2   //A
3   var id=$(this).attr("id");
4    
5   //B
6   var id=this.id;  
7   })

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.