write a js function: given page x, page y, width, height, find most ancestral DOM…
- User
write a js function: given page x, page y, width, height, find most ancestral DOM elements that are enclosed within the rect by z% error. most ancestral means if an element's parent satisfies the condition, then it is not most ancestral. (idk a better term) use bfs from document.body or if you have faster method please do so.
Pruning Strategy: Non-Exhaustive Mode (Default): If a parent element does not overlap with the target rectangle, its children are not checked. This significantly reduces the number of getBoundingClientRect calls, enhancing performance. Exhaustive Mode: All elements are checked regardless of their parents' positions. This ensures that absolutely positioned elements or other out-of-flow elements are not missed.
Exhaustive Search Option: An additional boolean parameter exhaustive is introduced to toggle between the default performant search and the exhaustive search. Additional Performance Considerations:
Early Bounding Rect Calculation: For each element, calculate its bounding rectangle only once per iteration to avoid redundant computations. Avoiding Layout Thrashing: By performing all necessary calculations within a single loop iteration, we minimize reflows and repaints. Efficient Queue Management: Using an array as a queue with push and shift for BFS.
Do not use array.shift as its slow. use custom data structure if needed. we aim for speed than short code.
- Assistant