Skip to content

Instantly share code, notes, and snippets.

@iiic
Created August 14, 2019 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iiic/7c2c750587d6afea35d52043f35fc09a to your computer and use it in GitHub Desktop.
Save iiic/7c2c750587d6afea35d52043f35fc09a to your computer and use it in GitHub Desktop.
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
Copy link
Author

iiic commented Aug 14, 2019

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