Created
August 27, 2022 15:55
-
-
Save nolanlawson/2d70b4f01d1d77ca47f069ad51177ff4 to your computer and use it in GitHub Desktop.
Layout thrashing browser optimization demo
This file contains hidden or 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
<!doctype html> | |
<html lang="en"> | |
<head> | |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" | |
integrity="sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M" crossorigin="anonymous"> | |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" | |
integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous"> | |
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap-theme.min.css" | |
integrity="sha384-6pzBo3FDv/PJ8r2KRkGHifhEocL+1X2rVCTTkUfGk7/0pbek5mMa1upzvWbrUbOZ" crossorigin="anonymous"> | |
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" | |
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous"> | |
<style id="theStyle"> | |
/* the goal of these selectors is to be stupidly unperformant */ | |
body:nth-child(2):not(.unused) div:nth-child(2) *[even="true"]:nth-child(even) + *[odd="true"]:nth-child(odd) { | |
font-size: 1.2em; | |
} | |
body:nth-child(2):not(.unused) div:nth-child(2) * :nth-child(even) { | |
background: aqua; | |
} | |
body:nth-child(2):not(.unused) div:nth-child(2) * :nth-child(odd) { | |
background: chocolate; | |
} | |
body:nth-child(2):not(.unused) div:nth-child(2) * :nth-child(even) + :nth-child(odd) { | |
font-weight: bold; | |
} | |
body:nth-child(2):not(.unused) div:nth-child(2) * :nth-child(odd) + :nth-child(even) { | |
font-size: 1.1em; | |
} | |
body:nth-child(2):not(.unused) div:nth-child(2) * [even="true"]:nth-child(even) + [odd="true"]:nth-child(odd) { | |
font-weight: bold; | |
} | |
body:nth-child(2):not(.unused) div:nth-child(2) *:nth-child(even) { | |
height: 40px; | |
overflow: hidden; | |
} | |
body:nth-child(2):not(.unused) div:nth-child(2) *:nth-child(odd) { | |
height: 30px; | |
overflow: hidden; | |
} | |
</style> | |
</head> | |
<body> | |
<template id="theHtml"> | |
<div> | |
<div class="row align-items-start" even="false"> | |
<div class="col" even="false"> | |
One of three columns | |
</div> | |
<div class="col" even="true"> | |
One of three columns | |
</div> | |
<div class="col" even="false"> | |
One of three columns | |
</div> | |
</div> | |
<div class="row align-items-center" even="true"> | |
<div class="col" even="false"> | |
One of three columns | |
</div> | |
<div class="col" even="true"> | |
One of three columns | |
</div> | |
<div class="col" even="false"> | |
One of three columns | |
</div> | |
</div> | |
<div class="row align-items-end" even="false"> | |
<div class="col" even="false"> | |
One of three columns | |
</div> | |
<div class="col" even="true"> | |
One of three columns | |
</div> | |
<div class="col" even="false"> | |
One of three columns | |
</div> | |
</div> | |
</div> | |
</template> | |
<div> | |
<button type="button" id="doThrash" style="all: revert">Thrash?</button> | |
<pre id="display"></pre> | |
</div> | |
<div id="container"> | |
</div> | |
<script type="module"> | |
for (let i = 0; i < 10; i++) { | |
container.appendChild(theHtml.content.cloneNode(true)) | |
} | |
for (let i = 0; i < 100; i++) { | |
const clone = theStyle.cloneNode(true) | |
clone.id = '' | |
document.head.appendChild(clone) | |
} | |
const doIt = () => { | |
const channel = new MessageChannel(); | |
channel.port1.onmessage = () => { | |
performance.mark('end') | |
performance.measure('total', 'start', 'end') | |
display.innerHTML += performance.getEntriesByType('measure').slice(-1)[0].duration + '\n' | |
} | |
requestAnimationFrame(() => { | |
performance.mark('start') | |
function thrash() { | |
const el = document.querySelector('.col') | |
performance.mark('loop') | |
for (let i = 0; i < 1000; i++) { | |
el.style.width = '1px' | |
el.getBoundingClientRect() | |
} | |
performance.measure('loop', 'loop') | |
} | |
thrash() | |
channel.port2.postMessage(undefined) | |
}) | |
} | |
doThrash.addEventListener('click', () => doIt(true)) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment