developit / developit/strip-dom-whitespace
Performance
- Dominant language
- JavaScript
- Stars
- 8
- Forks
- 1
- PR merge metrics
- No merged PRs in 30d
Description
I _think_ using a TreeWalker might be faster than iterating manually over the children of the passed node, but I'm not 100% certain as I haven't benchmarked it. You could do something like this:
```JS
const walk = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, null, false);
while (walk.nextNode()) {
doSomething(walk.currentNode);
}
```
This will parse a bunch of unnecessary nodes though, even with `NodeFilter.SHOW_TEXT`, so it might be even faster to even then specifically filter out nodes that we don't care about:
```JS
const INVALID_NODE_TYPES = [
'SCRIPT',
'STYLE',
'CANVAS',
'IMG',
'META',
'NOSCRIPT',
'VIDEO',
]
const walk = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, null, false);
while (walk.nextNode()) {
// Check that we don't parse tags which highly likely don't have visible/parseable text
if (INVALID_NODE_TYPES.indexOf(walk.currentNode.parentElement.tagName) === -1) {
doSomething(walk.currentNode);
}
}
```
(taken from my experience building a chrome extension that parses full webpages: https://github.com/mxstbr/convertr/blob/master/Source/javascripts/utils/getTextNodes.js – this used to crash when visiting Google/FB/... when manually iterating, not with a TreeWalker though!)
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by comparing the repository's current DOM traversal with the TreeWalker approach described in the issue, using the referenced getTextNodes.js as background. Benchmark representative pages and verify that excluded elements and whitespace handling remain correct; done means a measured improvement with no behavioral regression.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- frontend
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100