- From: Andrea Giammarchi <notifications@github.com>
- Date: Tue, 13 Oct 2020 04:59:50 -0700
- To: whatwg/dom <dom@noreply.github.com>
- Cc: Subscribed <subscribed@noreply.github.com>
- Message-ID: <whatwg/dom/issues/67/707690646@github.com>
FWIW, I've just refactored some code from this:
```js
function notifyIfMatchesXPath(query, callback) {
const flag = XPathResult.ORDERED_NODE_SNAPSHOT_TYPE;
const callback = () => {
const result = document.evaluate(query, document, null, flag, null);
for (let i = 0, {snapshotLength} = result; i < snapshotLength; i++)
callback(result.snapshotItem(i));
};
new MutationObserver(callback).observe(
document,
{characterData: true, childList: true, subtree: true}
);
callback();
}
```
to this:
```js
function notifyIfMatchesXPath(query, callback) {
const evaluator = new XPathEvaluator();
const expression = evaluator.createExpression(query, null);
const flag = XPathResult.ORDERED_NODE_SNAPSHOT_TYPE;
const callback = () => {
const result = expression.evaluate(document, flag, null);
for (let i = 0, {snapshotLength} = result; i < snapshotLength; i++)
callback(result.snapshotItem(i));
};
new MutationObserver(callback).observe(
document,
{characterData: true, childList: true, subtree: true}
);
callback();
}
```
assuming the cost of parsing, and validating, the XPath query would've removed from the mutation dance/equation, but I've discovered only recently `XPathEvaluator` and `createExpression`, and I'm sure if other developers new about it, its usage would be closer to the `document.evaluate` one, which is also now at 2.x%.
If the direction is to nuke `XPathEvaluator` though, I rather would like to know it before landing such refactoring, thanks.
--
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/67#issuecomment-707690646
Received on Tuesday, 13 October 2020 12:00:03 UTC