Created
April 15, 2021 12:27
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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