Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Created October 21, 2022 22:30
Show Gist options
  • Save nolanlawson/c1b690c16beead306ab2c4ac7e06c90a to your computer and use it in GitHub Desktop.
Save nolanlawson/c1b690c16beead306ab2c4ac7e06c90a to your computer and use it in GitHub Desktop.
Sibling selector benchmark
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Sibling selector benchmark</title>
</head>
<body>
<pre id="display"></pre>
<div id="container"></div>
<script type="module">
const params = new URLSearchParams(location.search)
const num = parseInt(params.get('num')) || 10000
const type = params.get('type') || 'sibling'
document.addEventListener('DOMContentLoaded', () => {
requestAnimationFrame(() => {
for (let i = 0; i < num; i++) {
container.appendChild(document.createElement('div'))
}
const style = document.createElement('style')
const combinator = type === 'sibling' ? '~' : type === 'descendant' ? ' ' : type === 'adjacentSibling' ? '+' : '>'
style.textContent = `
p ${combinator} * { color: red }
`
document.head.appendChild(style)
performance.mark('start')
// requestPostAnimationFrame polyfill
requestAnimationFrame(() => {
addEventListener('message', () => {
performance.measure('total', 'start')
display.innerHTML += `${performance.getEntriesByType('measure').slice(-1)[0].duration}ms\n`
}, {once: true})
postMessage('', '*')
})
})
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment