-
Notifications
You must be signed in to change notification settings - Fork 51
fix: add caching + reduce redundant array creation in getHierarchy #1194
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
✅ No documentation updates required. |
…mplitude-TypeScript into dpgraham/optimize-get-hierarchy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR optimizes the performance of the getHierarchy
and getNearestLabel
functions by implementing caching mechanisms and replacing inefficient array operations with direct iteration. The changes are motivated by performance tests showing that these functions are called multiple times on the same element within the same event loop.
- Adds caching for
getHierarchy
andgetNearestLabel
results that invalidates after the current event loop - Replaces
Array.from
andArray.prototype.filter
operations with direct for-loop iterations to reduce O(n) array creation overhead - Includes comprehensive performance test pages to validate the optimizations
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
File | Description |
---|---|
test-server/autocapture/performance-test.html | Performance test page with deeply nested DOM structure (15 levels, 12 siblings per level) |
test-server/autocapture/performance-test-extreme.html | Extreme performance test page with 100th descendant target and 100 siblings per row |
packages/plugin-autocapture-browser/src/hierarchy.ts | Adds caching mechanism and optimizes sibling iteration in getElementProperties and getHierarchy |
packages/plugin-autocapture-browser/src/helpers.ts | Adds caching mechanism for getNearestLabel function |
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
✅ No documentation updates required. |
Summary
getHierarchy
andgetNearestLabel
get executed multiple times (click handler, change handler) on the same element, within the same event loop, returning the same resultArray.from
andArray.prototype.filter
on both thechildren
andattributes
properties of Element. These methods create new arrays, where the creation of the arrays is O(n) both size and spacegetHierarchy
andgetNearestLabel
into one function so that we traverse an element's ancestry once instead of twiceChecklist