jquery Source Learning (ii) Sizzle part "turn"

Source: Internet
Author: User

First, the basic principle of sizzle

Sizzle is the name of the jquery selector engine module, which is independent from the 1.3 version and is used by many other JS libraries as the default selector engine.
First, the biggest feature of sizzle is fast. So why sizzle then the other engines were fast, because at that time the other engines were based on matching from left to right in a way to find, and sizzle just the opposite is from right to left to find.

Give a simple example ". A. B. C" To explain why sizzle is faster. This example, if found in order from left to right, obviously requires three traversal to end, which is to find in document first. A, then look in the results. B, and then look in the results of. B. C.
In this way, the time complexity is a*b*c, in fact, the time complexity O (n3). Note that O (N3) here only counts the traversal of the DOM, because this is particularly time-consuming for the operation of the PO.
Let's look at how sizzle is done, sizzle is looking right-to-left, that is, looking directly in the page. C, a candidate result set seed is obtained, and then the results of each of the seed's results are rejected by parents. A. B is the final result. This process actually only takes a single traversal of the DOM, and the rest of the filtering is quick to find directly along the parent pointer. This can be thought of as a time-complexity O (n) algorithm.
So, it can be seen that the sizzle that only need to traverse to find the DOM has obvious speed advantage in theory, and the Practice test proves it.

Second, the realization of sizzle

1, first call sizzle (selector, context) method

Things to do with this method:
1.1 Set the context first, then call rquickexpr regular to determine whether it is not a simple id/class/tag selector, if it is directly called the corresponding method can be obtained results
1.2 If it is not a simple selector, see if the QSA (or Queryselectorall) method is supported, and if supported, call the Queryselectorall method to fetch the result
1.3 Otherwise, there is no shortcut, call Select (Selector, context, results, seed)

2, call the Select (Selector, context, results, Seed) method

2.1 First Call Tokenize (selector) method to break selector into a single token
2.2 If there are no seed seeds, then 2.3
2.3 If the decomposed tokens have only one set, then depending on the tokens from right to left to find the seed, such as the selector is ". A. B. C", then first find all the. C elements in the candidate set seed, and all that is needed is to filter out the non-conforming elements in the seed.
2.4 The last call to compile (selector, match) (seed, context) rejects the non-qualifying elements in seed

3, call the Compile method

This method returns a function that is called after:
3.1, each element in the seed call Matcher Filter by one, the matching results into the results array. The implementation of the Matcher method, see 4

4,matcher method

The role of Matcher is to filter the candidate set seed, matcher the implementation of the process is to find an element constantly upward parentnode and determine whether the parentnode is satisfied with the condition, the return FALSE if the condition is not satisfied.

The general principle is this, specific to the following implementation details for the moment do not want to see one by one, if the series are all updated after the free time to improve this chapter. ^_^

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.