perf: improved efficiency of element scanning in eventmanager

This commit is contained in:
SethBurkart123
2025-08-17 11:02:41 +10:00
parent 16273cf012
commit 2ef8bb215a
+26 -2
View File
@@ -57,13 +57,37 @@ class EventManager {
return { unregister }; return { unregister };
} }
private buildSelector(options: EventListenerOptions): string | null {
if (options.textContent || options.customCheck) return null;
let selector = options.elementType || "";
if (options.id) {
selector += `#${CSS.escape(options.id)}`;
}
if (options.className) {
selector += `.${CSS.escape(options.className)}`;
}
return selector.trim() || null;
}
private async scanExistingElements( private async scanExistingElements(
options: EventListenerOptions, options: EventListenerOptions,
callback: (element: Element) => void, callback: (element: Element) => void,
): Promise<void> { ): Promise<void> {
const root = options.parentElement || document.documentElement; const root = options.parentElement || document.documentElement;
const elements = Array.from(root.getElementsByTagName("*")); const selector = this.buildSelector(options);
elements.unshift(root); let elements: Element[] = [];
if (selector) {
elements = Array.from(root.querySelectorAll(selector));
if (selector && root.matches && root.matches(selector)) {
elements.unshift(root);
}
} else {
elements = Array.from(root.getElementsByTagName("*"));
elements.unshift(root);
}
for (let i = 0; i < elements.length; i += this.chunkSize) { for (let i = 0; i < elements.length; i += this.chunkSize) {
const chunk = elements.slice(i, i + this.chunkSize); const chunk = elements.slice(i, i + this.chunkSize);