Compile selectors
eush77 opened this issue · comments
Currently selectors are interpreted when they execute. This is suboptimal because the same analyses must be performed each time and, more importantly, every new type of selector punishes all consumers regardless of whether they use it or not.
A better opportunity lies in compiling selectors to functions (agnostic of any actual unist tree) and having a module-local cache of those.
Could this be done by generalising an existing optimised CSS selector implementation on DOM nodes (like css-select) to unist nodes?
Probably… regarding css-select, it uses parent
link which is missing on Unist nodes (can be emulated but this could be slow; better to fix the algorithm) to traverse selectors in reverse order (to get from bar
to foo
in foo > bar
), and there's also of course some DOM-specific stuff (not much). I would say it's possible.
cssauron looks good, but it also requires .parent
, which is unfortunate.