Skip to content

Instantly share code, notes, and snippets.

@manrueda
Created April 15, 2021 12:27
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 manrueda/85b7a5a8d4ecbb97e8629abea50be3a0 to your computer and use it in GitHub Desktop.
Save manrueda/85b7a5a8d4ecbb97e8629abea50be3a0 to your computer and use it in GitHub Desktop.
This script will find all CSS statements and sort them by the impact that have on the current DOM
// Finds the most impactful CSS statements based on DOM impact
const allRules = Array.from(document.styleSheets).map(s => Array.from(s.rules).filter(e => e.selectorText)).flat()
const removePseudo = selector => selector.split(',').map(s => s.replace(/::[^ ,]*/g, '').trim()).filter(Boolean).join(', ')
const results = allRules.map(r => {
// removes the pseudo elements and pseudo classes
const impactedNodes = document.querySelectorAll(removePseudo(r.selectorText)).length;
return {
impactedNodes,
selectorText: r.selectorText,
selectorText: r.selectorText,
cssText: r.style.cssText,
}
}).sort((a,b) => b.impactedNodes - a.impactedNodes)
console.log(results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment