Most implementations of CSS-like selectors (that is, patterns that may match paths in a tree) seem to use a bottom-up approach. That is, for each node in the tree, they check for a match against the rightmost element of the selector pattern. If the node matches, they search up its ancestors checking for a match against the second-to-rightmost element of the selector pattern, and so on.

My question is, why is this more efficient than starting from the top and working down (left-to-right in the selector pattern)? In both approaches it seems like you have to examine every path, and both approaches can benefit from short-circuiting.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.