Avoid using Element.classList in DOM surveyor

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/discussions/2076
This commit is contained in:
Raymond Hill 2022-04-03 13:13:20 -04:00
parent 3d6aead585
commit 1423330703
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
1 changed files with 14 additions and 13 deletions

View File

@ -1024,7 +1024,6 @@ vAPI.DOMFilterer = class {
// Extract all classes/ids: these will be passed to the cosmetic // Extract all classes/ids: these will be passed to the cosmetic
// filtering engine, and in return we will obtain only the relevant // filtering engine, and in return we will obtain only the relevant
// CSS selectors. // CSS selectors.
const reWhitespace = /\s/;
// https://github.com/gorhill/uBlock/issues/672 // https://github.com/gorhill/uBlock/issues/672
// http://www.w3.org/TR/2014/REC-html5-20141028/infrastructure.html#space-separated-tokens // http://www.w3.org/TR/2014/REC-html5-20141028/infrastructure.html#space-separated-tokens
@ -1039,19 +1038,21 @@ vAPI.DOMFilterer = class {
queriedIds.add(s); queriedIds.add(s);
}; };
// https://github.com/uBlockOrigin/uBlock-issues/discussions/2076
// Performance: avoid using Element.classList
const classesFromNode = (node, out) => { const classesFromNode = (node, out) => {
const s = node.className; const s = node.getAttribute('class');
if ( typeof s !== 'string' || s.length === 0 ) { return; } if ( typeof s !== 'string' ) { return; }
if ( reWhitespace.test(s) === false ) { const len = s.length;
if ( queriedClasses.has(s) ) { return; } for ( let beg = 0, end = 0, token = ''; beg < len; beg += 1 ) {
out.push(s); end = s.indexOf(' ', beg);
queriedClasses.add(s); if ( end === beg ) { continue; }
return; if ( end === -1 ) { end = len; }
} token = s.slice(beg, end);
for ( const s of node.classList.values() ) { beg = end;
if ( queriedClasses.has(s) ) { continue; } if ( queriedClasses.has(token) ) { continue; }
out.push(s); out.push(token);
queriedClasses.add(s); queriedClasses.add(token);
} }
}; };