I don't understand whatever algorithm document.querySelector uses for `,`-separated multi selectors. Like, how it gets to returning things in the order it does :|

Follow

@zkat doesn't it just walk the tree and return all/the first node(s) which match the selector string?

@Lady I mean yeah kinda but also after implementing an algorithm that does this myself, I'm like "how tf are you doing all the bookkeeping for this?"

@zkat my understanding (not a browser implementer so i could be wrong) is that all the optimization is in the "does this node match the selector string" bit and the actual querying is a straightforward traversal of nodes in document order, returning the ones which match

like you could maybe get a little faster by optimizing certain queries to not do a full traversal, keeping track of each node's position "in document order", and doing a sort at the end but i'm not sure it's worth it…

i would expect the more expensive operation in most documents is probably doing the "does this node match one of these selectors" boolean test

@zkat (browsers have to traverse the nodes in document order and determine which ones match which selector strings in order to render the document anyway, so they definitely have fast tooling for this)

Sign in to participate in the conversation
📟🐱 GlitchCat

A small, community‐oriented Mastodon‐compatible Fediverse (GlitchSoc) instance managed as a joint venture between the cat and KIBI families.