Skip to content

Conversation

@divya2212001
Copy link

What:

Optimize container lookup logic in src/pure.js by replacing forEach with find() to enable an early exit and reduce unnecessary iteration when retrieving an existing root entry.

Why:

forEach continues executing even after a match is found, doing extra iterations

find() stops immediately, improving performance

clearer semantic intent — we want the first match

beneficial in scenarios with many mounted containers

preserves the existing behavior and requires no API changes

How:

Replaced the container reuse lookup code:

mountedRootEntries.forEach(rootEntry => {
  if (rootEntry.container === container) {
    root = rootEntry.root
  }
})

with:

const entry = mountedRootEntries.find(
  entry => entry.container === container
)

if (entry) {
  root = entry.root
}

This ensures faster lookup and avoids unnecessary looping.

Checklist:

  • Documentation added to the docs site (N/A — internal optimization)

  • Tests (existing tests already cover container reuse behavior)

  • TypeScript definitions updated (N/A — no type changes)

  • Ready to be merged

Additional comments:

This is a safe, contained performance optimization with no functional changes to the public interface. The internal logic is now more efficient and easier to maintain.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 2, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 1b196ea:

Sandbox Source
react-testing-library-examples Configuration

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