Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save motss/b640918272c2b9e4cbeeedf29cad1448 to your computer and use it in GitHub Desktop.

Select an option

Save motss/b640918272c2b9e4cbeeedf29cad1448 to your computer and use it in GitHub Desktop.
Simple benchmarks - `Node.childNodes` vs `Document.querySelectorAll`
const df = Array.from(Array(10), (_, i) => {
const a = document.createElement('div');
a.classList.add('item');
return a;
}).reduce((p, n) => (p.appendChild(n), p), document.createDocumentFragment());
const container = document.createElement('div');
container.classList.add('container');
document.body.appendChild(container);
container.appendChild(df.cloneNode(true));
/** NOTE: Warm-up */
for (let i = 0; i < 1e1; i += 1) { document.body.querySelector('.container').childNodes; document.body.querySelectorAll('.item'); }
for (let i = 0; i < 1e2; i += 1) { document.body.querySelector('.container').childNodes; document.body.querySelectorAll('.item'); }
for (let i = 0; i < 1e3; i += 1) { document.body.querySelector('.container').childNodes; document.body.querySelectorAll('.item'); }
console.time('childNodes');
a = document.body.querySelector('.container').childNodes;
console.timeEnd('childNodes');
console.time('querySelectorAll');
b = document.querySelectorAll('.item');
console.timeEnd('querySelectorAll');
console.log([a, b]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment