Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
it's part of Chrome Lighthouse test, but this is separated
'use strict';
function lighthouseDomTest ( /** @type {Element} */ rootElement )
{
let maxDepth = 0;
let maxDepthElement = null;
let mostChildren = 0;
let mostChildrenElement = null;
let allElements = rootElement.querySelectorAll( '*' );
allElements.forEach( ( element ) =>
{
let currentElement = element;
if ( !currentElement.children.length ) {
let steps = 0;
while ( currentElement !== rootElement ) {
steps++;
currentElement = currentElement.parentElement;
}
if ( maxDepth < steps ) {
maxDepth = steps;
maxDepthElement = element;
}
}
if ( element.children.length > mostChildren ) {
mostChildren = element.children.length;
mostChildrenElement = element;
}
} );
return {
'totalElementNodes': allElements.length,
'maxDepth': maxDepth,
'maxDepthElement': maxDepthElement,
'mostChildren': mostChildren,
'mostChildrenInElement': mostChildrenElement,
};
}
lighthouseDomTest( document.body );
@iiic

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment