Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(jsx/dom): fix performance issue with adding many new node listings * First time rendering does not need to look for elements from child nodes ** Avoid unnecessary checks with `node.pP`. * Sibling elements are always searched, so there is no need for a for loop. ** Need only `findInsertBefore(node.vC[0])` * test(jsx/dom): add test for performance

authored by Taku Amano · committed by GitHub · parent 3a56ec5 · Browse files · 2 files+23 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+23 added−18 removed

  • tests:+18 added−0 removed
  • other:+5 added−18 removed

2 files+23 added−18 removed

modified:src/jsx/dom/index.test.tsx+18 added
115 unmodified lines
116 render(<App />, root)117 expect(root.innerHTML).toBe('Hello')118 })119120 describe('performance', () => {121 it('should be O(N) for each additional element', () => {122 const App = () => (123 <>124 {Array.from({ length: 1000 }, (_, i) => (125 <div>126 <span>{i}</span>127 </div>128 ))}129 </>130 )131 render(<App />, root)132 expect(root.innerHTML).toBe(133 Array.from({ length: 1000 }, (_, i) => `<div><span>${i}</span></div>`).join('')134 )135 })136 })137138 describe('attribute', () => {139 it('simple', () => {140 const App = () => <div id='app' class='app' />141 render(<App />, root)
2160 unmodified lines
modified:src/jsx/dom/render.ts+5 added-18 removed
269 unmodified lines
270 })271}272273const findInsertBefore = (node: Node | undefined): SupportedElement | Text | null => {274 if (!node) {275 return null276 } else if (node.tag === HONO_PORTAL_ELEMENT) {277 return findInsertBefore(node.nN)278 } else if (node.e) {279 return node.e280 }281282 if (node.vC) {283 for (let i = 0, len = node.vC.length; i < len; i++) {284 const e = findInsertBefore(node.vC[i])285 if (e) {286 return e287 }288 }289 }290291 return findInsertBefore(node.nN)274 return !node275 ? null276 : node.tag === HONO_PORTAL_ELEMENT277 ? findInsertBefore(node.nN)278 : node.e || (node.vC && node.pP && findInsertBefore(node.vC[0])) || findInsertBefore(node.nN)279}280281const removeNode = (node: Node): void => {282 if (!isNodeString(node)) {
417 unmodified lines