Skip to content
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

wip: dnm testing size and verdaccio shadowdom for size testing #7548

Open
wants to merge 147 commits into
base: main
Choose a base branch
from

Conversation

snowystinger
Copy link
Member

Closes

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices

📝 Test Instructions:

🧢 Your Project:

MahmoudElsayad and others added 30 commits March 11, 2024 23:12
Update domHelpers.test.js.
Add Tests for FocusScope.test.js.
New helper util `getRootBody`.
Fix `useRestoreFocus` issue.
Add new DOM util `getDeepActiveElement`.
update `useFocus` - `useFocusWithin` - `usePress`.
Test for `focusSafely`.
@rspbot
Copy link

rspbot commented Dec 23, 2024

@rspbot
Copy link

rspbot commented Dec 23, 2024

@rspbot
Copy link

rspbot commented Dec 23, 2024

1 similar comment
@rspbot
Copy link

rspbot commented Dec 23, 2024

@snowystinger
Copy link
Member Author

There appears to be no appreciable difference between node.contains and event.composedPath
run on the same dom of 100 nested divs, event fired on the lowest one, checking from document.body and with the same number of runs, times are consistently taking 10s to run in jest and differ from each other by 100ms in both directions depending on the run

describe('checking if node is contained', () => {
  let user;
  beforeAll(() => {
    user = userEvent.setup({delay: null, pointerMap});
  });
  it.only("benchmark native contains", async () => {
    let Component = (props) => {
      if (props.depth === 0) {
        return <div data-testid="hello">hello</div>
      }
      return <div><Component depth={props.depth -1}/></div>
    }
    let {getByTestId} = render(
      <Component depth={100} />
    );
    let target = getByTestId('hello');
    let handler = jest.fn((e) => {
      expect(e.currentTarget.contains(e.target)).toBe(true);
    });
    document.body.addEventListener('click', handler);
    for (let i = 0; i < 50; i++) {
      await user.click(target);
      expect(handler).toHaveBeenCalledTimes(i + 1);
    }
  });
  it.only("benchmark nodeContains", async () => {
    let Component = (props) => {
      if (props.depth === 0) {
        return <div data-testid="hello">hello</div>
      }
      return <div><Component depth={props.depth -1}/></div>
    }
    let {getByTestId} = render(
      <Component depth={100} />
    );
    let target = getByTestId('hello');
    let handler = jest.fn((e) => {
      expect(nodeContains(e.currentTarget, e.composedPath()[0])).toBe(true);
    });
    document.body.addEventListener('click', handler);
    for (let i = 0; i < 50; i++) {
      await user.click(target);
      expect(handler).toHaveBeenCalledTimes(i + 1);
    }
  });
});

@snowystinger
Copy link
Member Author

snowystinger commented Jan 16, 2025

There is a large difference in amount of time for shadow tree walker

Native:
Time taken for 10000 iterations: 41.57495799660683ms

Shadow:
Time taken for 10000 iterations: 1139.0474170148373ms

The good news is that usually we don't run it this far or as many times. It's typically in response to a user action with the walker destination not very far away and with 1 iteration being roughly 0.1ms, that's plenty of time.
We can still separate these so that not everyone is hit with the performance degradation, but I think we're ok to do that in followup, this should be ok for an initial release.

describe.only('speed test', () => {
  let Component = (props) => {
    if (props.depth === 0) {
      return <div data-testid="hello">hello</div>
    }
    return <div><Component depth={props.depth -1}/></div>
  }
  it.each`
  Name | createTreeWalker
  ${'native'} | ${() => document.createTreeWalker(document.body, NodeFilter.SHOW_ALL)}
  ${'shadow'} | ${() => createShadowTreeWalker(document, document.body)}
  `('$Name', ({createTreeWalker}) => {
    render(
      <>
        <div id="div-one" />
        <Component depth={100} />
        <input id="input-one" />
        <div id="div-two" />
        <Component depth={100} />
        <input id="input-two" />
        <div id="div-three" />
        <Component depth={100} />
        <input id="input-three" />
        <div id="div-four" />
        <Component depth={100} />
      </>
    );
    let walker = createTreeWalker();
    let start = performance.now();
    for (let i = 0; i < 10000; i++) {
      walker.firstChild();
      walker.nextNode();
      walker.previousNode();
      walker.lastChild();
    }
    let end = performance.now();
    console.log(`Time taken for 10000 iterations: ${end - start}ms`);
  });
});

@snowystinger snowystinger marked this pull request as ready for review January 16, 2025 02:53
@rspbot
Copy link

rspbot commented Jan 16, 2025

@rspbot
Copy link

rspbot commented Jan 16, 2025

@rspbot
Copy link

rspbot commented Jan 16, 2025

@rspbot
Copy link

rspbot commented Jan 16, 2025

1 similar comment
@rspbot
Copy link

rspbot commented Jan 16, 2025

@rspbot
Copy link

rspbot commented Jan 16, 2025

@rspbot
Copy link

rspbot commented Jan 17, 2025

@rspbot
Copy link

rspbot commented Jan 17, 2025

@rspbot
Copy link

rspbot commented Jan 17, 2025

## API Changes

@react-aria/focus

/@react-aria/focus:isFocusable

 isFocusable {
-  element: HTMLElement
+  element: Element
   returnVal: undefined
 }

@react-aria/utils

/@react-aria/utils:createShadowTreeWalker

+createShadowTreeWalker {
+  doc: Document
+  root: Node
+  whatToShow?: number
+  filter?: NodeFilter | null
+  returnVal: undefined
+}

/@react-aria/utils:ShadowTreeWalker

+ShadowTreeWalker {
+  constructor: (Document, Node, number, NodeFilter | null) => void
+  currentNode: Node
+  doc: Document
+  filter: NodeFilter | null
+  firstChild: () => Node | null
+  lastChild: () => Node | null
+  nextNode: () => Node | null
+  previousNode: () => Node | null
+  root: Node
+  whatToShow: number
+}

/@react-aria/utils:getActiveElement

+getActiveElement {
+  doc: Document
+  returnVal: undefined
+}

/@react-aria/utils:getEventTarget

+getEventTarget {
+  event: any
+  returnVal: undefined
+}

/@react-aria/utils:nodeContains

+nodeContains {
+  node: Node | null | undefined
+  otherNode: Node | null | undefined
+  returnVal: undefined
+}

/@react-aria/utils:isShadowRoot

+isShadowRoot {
+  node: Node | null
+  returnVal: undefined
+}

@rspbot
Copy link

rspbot commented Jan 17, 2025

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.

5 participants