testing-library / testing-library/react-testing-library

perf: optimize container lookup with early exit

Open
#1,430 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
19.7k
Forks
1.2k
Avg merge
3d 16h
Merged PRs (30d)
1

Description

eplaced an inefficient forEach loop with the find() method in the
render()
function's container lookup logic, enabling early exit when a matching container is found.

Type of Change
Bug fix (performance optimization)
New feature
Breaking change
Documentation update
The Problem
In
src/pure.js
lines 286-293, when reusing an existing container, the code used forEach to search through mountedRootEntries:

javascript
mountedRootEntries.forEach(rootEntry => {
if (rootEntry.container === container) {
root = rootEntry.root
}
})
The forEach method continues iterating through all entries even after finding the matching container. This is inefficient because:

It performs unnecessary iterations after the match is found
Performance degrades as more containers are mounted
The code doesn't communicate the intent to find a single entry
The Solution
Replaced forEach with the find() method which stops iteration immediately upon finding a match:

javascript
const rootEntry = mountedRootEntries.find(
rootEntry => rootEntry.container === container,
)
if (rootEntry) {
root = rootEntry.root
}
This change:

⚡ Stops searching immediately when a match is found (early exit optimization)
📈 Improves performance, especially with multiple mounted containers
🎯 Makes the code intent clearer - we're looking for one specific entry
✅ Uses a more idiomatic JavaScript pattern

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/pure.js lines 286-293 and inspect the render() container lookup using mountedRootEntries. The work is done when the lookup can stop after finding the matching container while preserving the existing root reuse behavior; run the repository's existing tests to verify it.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, react
Domain
performance
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.