Skip to content

Instantly share code, notes, and snippets.

@runspired
Created September 12, 2023 05:28
Show Gist options
  • Save runspired/1a7f23abcc1f0e1fabc7993b5bb4d052 to your computer and use it in GitHub Desktop.
Save runspired/1a7f23abcc1f0e1fabc7993b5bb4d052 to your computer and use it in GitHub Desktop.
Element Scan
function timedGet(selector) { let start = performance.now(); let all = document.querySelectorAll(selector); let end = performance.now(); return { start, end, all }; }
function iterateAll(selector, cb) {
let data = timedGet(selector);
for (let e of data.all) cb(e);
let end = performance.now();
console.log({ start: data.start, iter: data.end, end, duration: end - data.start })
}
seen2 = new Set(); iterateAll('*', (e) => seen2.add(e));
function walk(element, cb) {
cb(element);
for (let e of element.children) walk(e, cb);
}
function walkAll(cb) {
let start = performance.now();
walk(document.body, cb);
let end = performance.now();
console.log({ start, end, duration: end - start });
}
seen = new Set(); walkAll((e) => seen.add(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment