Skip to content

Conversation

daniel-graham-amplitude
Copy link
Collaborator

@daniel-graham-amplitude daniel-graham-amplitude commented Jul 4, 2025

Summary

  • Adds 2 test pages that do performance tests (stress tests) on autocapture. They're both checkboxes that are deeply nested inside of a tree where each parent has many siblings and many attributes.
  • Optimizes performance by adding caching.
    • getHierarchy and getNearestLabel get executed multiple times (click handler, change handler) on the same element, within the same event loop, returning the same result
    • This adds a cache so that if they get called within the same event loop, it memoizes the result. If they happen on the same event loop the contents are expected to be the same because the DOM hasn't had a chance to re-render yet
  • Replaces array creation with array iterations
    • We use Array.from and Array.prototype.filter on both the children and attributes properties of Element. These methods create new arrays, where the creation of the arrays is O(n) both size and space
    • This refactors it so that instead of creating new arrays, it just runs a for-loop over the original array, and performs the same calculations (note that the tests are unchanged and still passing, indicating the behaviour is unchanged)
    • This changes it from running 3 O(n) runtime operations + 2 O(n) space operations down to just 1 O(n) runtime operation.
  • Next steps
    • Consolidate getHierarchy and getNearestLabel into one function so that we traverse an element's ancestry once instead of twice
    • Subject to agreement from team, put limits on how many DOM traversals we do (limit ancestor count, limit capture on elements that have high sibling counts and high attribute counts)

Checklist

  • Does your PR title have the correct title format?
  • Does your PR have a breaking change?: No

@daniel-graham-amplitude daniel-graham-amplitude marked this pull request as draft July 4, 2025 16:48
Copy link

promptless bot commented Jul 4, 2025

✅ No documentation updates required.

@daniel-graham-amplitude daniel-graham-amplitude changed the title fix: reduce redundant array creation in getHierarchy fix: add caching + reduce redundant array creation in getHierarchy Jul 16, 2025
Copy link
Contributor

@Copilot Copilot AI left a 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 and getNearestLabel results that invalidates after the current event loop
  • Replaces Array.from and Array.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

@daniel-graham-amplitude daniel-graham-amplitude marked this pull request as ready for review July 16, 2025 18:48
Copy link

promptless bot commented Jul 16, 2025

✅ No documentation updates required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant