Skip to content

perf: optimize container lookup with early exit #1430

@Bitshifter-9

Description

@Bitshifter-9

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions