WebKit .querySelectorAll()

NOTE: This page is a recollection of what I've written previously. I do not guarantee it's accuracy.

The querySelectorAll and related DOM methods are implemented somewhat differently from the CSS engine despite sharing syntax, because whereas the CSS engine matches each element against several selectors, querySelectorAll matches each selector against numerous elements. However it does reuse some of the some code.

For one it uses the same parser, before preprocessing the results in order to determine an optimal codepath to use and caching the result. And for any non-trivial selector it’ll reuse the same selector JIT.

Those codepaths may refer to hashtable or array indices attached to the “scope” of each and every element in the DOM, or they may be simply tighter codepaths around testing every descendant element. Sometimes they even cache additional information/optimizations in order to run faster next time.