JQuery selectors are everywhere. Do you know their performance ?, Jquery Selector

Source: Internet
Author: User

JQuery selectors are everywhere. Do you know their performance ?, Jquery Selector

Nowadays, jQuery is becoming more and more popular on the website. Therefore, even a little bit of understanding, advantages, and shortcomings of jQuery are more and more worthy of research and discussion. Recently, I have made some small experiments on jQuery selector usage to demonstrate Which of the following is more efficient and worth using different jQuery selectors under different circumstances.

First, introduce the jquery file provided by google and the small plug-in firejspt for testing in the head of each test page.

<! -- Introduce the library file of FireJSPT --> <script type = "text/javascript" src = "firejspt. js"> </script> <! -- Introduce jQuery library files of version 1.44 provided by google. In fact, it doesn't matter which version, haha --> <script type = "text/javascript" src = "https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"> </script>


1. The most common id selector and class selector
Copy the following code 200 times and place it in the body tag.

<Div id = "ilian"> comparison id selector and class selector </div> <div class = "ilian"> comparison id selector and class selector </div>


The JS Code used for this test is as follows:

Function ilianTest01 () {$ ('# ilian '). click (function () {alert ('Hello World') ;});} function ilianTest02 () {$ ('. ilian '). click (function () {alert ('Hello World') ;});}/* call two functions for testing */$ (function () {jspt. test (function () {ilianTest01 () ;}); jspt. test (function () {ilianTest02 ();});});


The test results are as follows:


The efficiency advantage of id selector compared with class selector is very good .....
With this test address: http://www.threesnow.com/code/090/ilian_01.html


2. the hierarchical selector is frequently used when selecting tags. In this comparison test, the sub-label symbols ">" and children are used directly.

Put the following code into the body tag and copy the li tag 500 times.

<Ul id = "ilian"> <li> compare the direct sub-tag symbols ">" and children </li> <li> compare the direct sub-tag symbols ">" and children </ li> <li> compare the direct sub-label symbols ">" and children </li> <! -- Omitted 497 times --> </ul>


The JS Code used for this test is as follows:

Function ilianTest01 () {$ ('# ilian> li '). click (function () {alert ('Hello World') ;});} function ilianTest02 () {$ ('# ilian '). children ('lil '). click (function () {alert ('Hello World') ;});}/* call two functions for testing */$ (function () {jspt. test (function () {ilianTest01 () ;}); jspt. test (function () {ilianTest02 ();});});


Test results:


The children selector is better than the direct sub-tag symbol selector.
With this test address: http://www.threesnow.com/code/090/ilian_02.html

Limited by the length of the article, this article only shows the most basic tests, and the above tests are all tested in a simple environment, the test results do not represent an absolute conclusion.

If the article is useful, do not forget to recommend it ~~

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.