Traversal of jQuery code optimization

Source: Internet
Author: User

Collections correspond to the use of selector to search for elements on the page. The working mechanism behind the jQuery traversal operation is also very distinctive. I understand the working mechanism behind jQuery's DOM traversal, and can consciously avoid unnecessary repeated operations when writing code, thus improving the code performance. This article will start with jQuery's traversal mechanism and briefly discuss the problem of optimizing jQuery code.

JQuery object Stack

JQuery maintains a jQuery object stack. Each Traversal method will find a new set of elements (a jQuery object), and jQuery will push this set of elements into the stack. Each jQuery object has three attributes: context, selector, and prevObject. The prevObject attribute points to the previous object in the object stack, this attribute can be used to trace back to the original DOM element set.

JQuery provides two very useful methods for us to operate on this internal object Stack:

◆ End ()

◆ AndSelf ()

Calling the first method simply pops up an object (the result is to return to the previous jQuery object ). The second method is more interesting. Calling it will trace back a position in the stack, and then combine the elements in the two positions, push the new and combined element set to the top of the stack.

Using this DOM element stack can reduce repeated queries and traversal operations, while reducing repeated operations is also the key to optimizing jQuery code performance.

Optimization example

The following is a function (the front and back code are omitted) used to achieve the stripe Effect of table rows:

 
 
  1. function stripe() {  
  2.     $('#news').find('tr.alt').removeClass('alt');  
  3.     $('#news tbody').each(function() {  
  4.         $(this).children(':visible').has('td')  
  5.         .filter(':group(3)').addClass('alt');  
  6.     });  

The stripe () function uses the ID selector twice # news search element: one is used to delete the class from the row with the alt class, the other is to add this class to the newly selected row.

There are two methods to optimize this function. One is concatenation.

Concatenation

Concatenation optimization uses jQuery's internal object stack and. end () method. The optimized code is as follows:

 
 
  1. function stripe() {  
  2.     $('#news').  
  3.     find('tr.alt').removeClass('alt').end()  
  4.     find('tbody').each(function() {  
  5.         $(this).children(':visible').has('td')  
  6.         .filter(':group(3)').addClass('alt');  
  7.     });  

The first call. find () will push the table row to the stack, and then. the end () method pops up these rows for the next call. find () is still executed on the # news table. In this way, the two selector searches are reduced to one.

Another optimization method is cache.

Cache

The so-called cache stores the results of previous operations for future reuse. The optimized code is as follows:

 
 
  1. var $news = $('#news');  
  2. function stripe() {  
  3.     $news.find('tr.alt').removeClass('alt');  
  4.     $news.find('tbody').each(function() {  
  5.         $(this).children(':visible').has('td')  
  6.         .filter(':group(3)').addClass('alt');  
  7.     });  

Compared with the concatenation method, the caching method is too long because an additional variable is created to save the jQuery object. But from another perspective, this method can completely separate the two operations on the selected elements in the code, which may meet our needs in other situations. Similarly, because you can save the selected elements outside the stripe () function, you can avoid repeated query of the selection operator every time you call a function.

Conclusion

Using jQuery's internal object stack and cache can reduce repeated DOM queries and traversal, thus improving script execution speed.

Because the selection of elements on the page by ID is extremely fast, there will be no significant performance difference between the above two examples before and after optimization. In actual coding, you should select the most readable and easy to maintain mode. If you encounter performance bottlenecks, the above optimization technologies can achieve immediate results.

Original article: http://www.ituring.com.cn/article/382

Related Article

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.