Skip to content

Instantly share code, notes, and snippets.

@jantimon
Created May 31, 2024 07:01
Show Gist options
  • Save jantimon/401795403064107315c02372be35ed39 to your computer and use it in GitHub Desktop.
Save jantimon/401795403064107315c02372be35ed39 to your computer and use it in GitHub Desktop.
test cases for browser performance tests
// tested on: https://www.youtube.com/results?search_query=chrome+performance
const styleElement = document.createElement("style");
styleElement.innerHTML = `
@property --unused-property-no-inherit { inherits: false }
*, *:before, *:after { box-sizing: inherit }
.opacity-30 {
opacity: 0.3;
}
`
document.head.appendChild(styleElement);
const styleSheet = styleElement.sheet;
setTimeout(() => {
performance.mark("add non-existing class to <html>");
console.log("add non-existing class to <html>");
document.querySelector("html").classList.add("unused-class")
}, 1000);
setTimeout(() => {
performance.mark("add class with opacity 0.3 to <html>");
console.log("add class with opacity 0.3 to <html>");
document.querySelector("html").classList.add("opacity-30")
}, 2000);
setTimeout(() => {
performance.mark("set <html> to opacity 0.4");
console.log("add to <html> opacity: 0.4");
styleSheet.insertRule("html { opacity: 0.4 }", styleSheet.cssRules.length);
}, 3000);
setTimeout(() => {
performance.mark("set <html> to opacity 0.5 with @media screen");
styleSheet.insertRule("@media screen { html { opacity: 0.5 } }", styleSheet.cssRules.length);
}, 4000);
setTimeout(() => {
performance.mark("set <html> to --unused-property: 999");
console.log("set <html> to --unused-property: 999");
styleSheet.insertRule("html { --unused-property: 999 }", styleSheet.cssRules.length);
}, 5000);
setTimeout(() => {
performance.mark("update <html> --unused-property to 111");
console.log("update <html> --unused-property to 111");
styleSheet.insertRule("html { --unused-property: 111 }", styleSheet.cssRules.length);
}, 6000);
setTimeout(() => {
performance.mark("set <html> to --unused-property-no-inherit: 999");
console.log("set <html> to -unused-property-no-inherit: 999");
styleSheet.insertRule("html { --unused-property-no-inherit: 999 }", styleSheet.cssRules.length);
}, 7000);
@jantimon
Copy link
Author

Chrome Firefox
classList.add("unused-class") 460 µs 22 µs
classList.add("opacity-30") 410 µs 31500 µs
insertRule("html { opacity: 0.4 }") 3340 µs 499 µs
insertRule("@media screen { html { opacity: 0.5 } }") 19860 µs 277 µs
insertRule("html { --unused-property: 999 }") 23640 µs 13100 µs
insertRule("html { --unused-property: 111 }") 19810 µs 23100 µs
insertRule("html { --unused-property-no-inherit: 999 }") 19660 µs 12900 µs

timings

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