Cap size of js hints and do some GC

Should fix #5230
This commit is contained in:
Oliver Blanthorn 2026-07-05 14:22:41 +02:00
parent 386efca4ef
commit 052f3c3f33
No known key found for this signature in database
GPG key ID: 2BB8C36BB504BFF3
2 changed files with 24 additions and 0 deletions

View file

@ -1234,6 +1234,7 @@ export function hintables(
)
const hintables: Hintables[] = [{ elements: elems }]
if (withjs) {
DOM.pruneHintworthyJSElems()
hintables.push({
elements: changeHintablesToLargestChild(
Array.from(DOM.hintworthy_js_elems).filter(

View file

@ -418,6 +418,21 @@ export function compareElementArea(a: HTMLElement, b: HTMLElement): number {
}
export const hintworthy_js_elems: Set<Element> = new Set()
const MAX_HINTWORTHY_JS_ELEMS = 1000
const HINTWORTHY_JS_ELEMS_PRUNE_INTERVAL = 100
let hintworthy_js_elems_additions = 0
export function pruneHintworthyJSElems() {
for (const elem of hintworthy_js_elems) {
if (!elem.isConnected) {
hintworthy_js_elems.delete(elem)
}
}
while (hintworthy_js_elems.size > MAX_HINTWORTHY_JS_ELEMS) {
hintworthy_js_elems.delete(hintworthy_js_elems.values().next().value)
}
hintworthy_js_elems_additions = 0
}
/** Adds or removes an element from the hintworthy_js_elems array of the
* current tab.
@ -474,6 +489,14 @@ export function registerEvListenerAction(
case "mouseover":
if (add) {
hintworthy_js_elems.add(elem)
hintworthy_js_elems_additions += 1
if (
hintworthy_js_elems_additions >=
HINTWORTHY_JS_ELEMS_PRUNE_INTERVAL ||
hintworthy_js_elems.size > MAX_HINTWORTHY_JS_ELEMS
) {
pruneHintworthyJSElems()
}
} else {
// Possible bug: If a page adds an event listener for "click" and
// "mousedown" and removes "mousedown" twice, we lose track of the