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

fix(jsx/dom): fix performance issue with adding many new node listings #3205

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/jsx/dom/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ describe('DOM', () => {
expect(root.innerHTML).toBe('Hello')
})

describe('performance', () => {
it('should be O(N) for each additional element', () => {
const App = () => (
<>
{Array.from({ length: 1000 }, (_, i) => (
<div>
<span>{i}</span>
</div>
))}
</>
)
render(<App />, root)
expect(root.innerHTML).toBe(
Array.from({ length: 1000 }, (_, i) => `<div><span>${i}</span></div>`).join('')
)
})
})

describe('attribute', () => {
it('simple', () => {
const App = () => <div id='app' class='app' />
Expand Down
23 changes: 5 additions & 18 deletions src/jsx/dom/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,24 +271,11 @@ const getNextChildren = (
}

const findInsertBefore = (node: Node | undefined): SupportedElement | Text | null => {
if (!node) {
return null
} else if (node.tag === HONO_PORTAL_ELEMENT) {
return findInsertBefore(node.nN)
} else if (node.e) {
return node.e
}

if (node.vC) {
for (let i = 0, len = node.vC.length; i < len; i++) {
const e = findInsertBefore(node.vC[i])
if (e) {
return e
}
}
}

return findInsertBefore(node.nN)
return !node
? null
: node.tag === HONO_PORTAL_ELEMENT
? findInsertBefore(node.nN)
: node.e || (node.vC && node.pP && findInsertBefore(node.vC[0])) || findInsertBefore(node.nN)
}

const removeNode = (node: Node): void => {
Expand Down