Skip to content
Myr

perf(jsx/dom): flatten children once at the start to avoid repeated flattening (#4730)

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

* perf(jsx/dom): flatten children once at the start to avoid repeated flattening * test(jsx/dom): Move the test to the correct position location and add a regression test

authored by Taku Amano · committed by GitHub · parent 11c7828 · Browse files · 2 files+74 added−19 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+74 added−19 removed

  • tests:+73 added−18 removed
  • other:+1 added−1 removed

2 files+74 added−19 removed

modified:src/jsx/dom/index.test.tsx+73 added-18 removed
117 unmodified lines
118 render(<App />, root)119 expect(root.innerHTML).toBe('Hello')120 })121122 it('render with empty array followed by non-empty array', () => {123 const tags: string[] = []124 const terms: string[] = ['hello']125 const App = () => (126 <div>127 {tags.map((x) => (128 <span>{x}</span>129 ))}130 {terms.map((x) => (131 <span>{x}</span>132 ))}133 <input type='text' />134 </div>135 )136 render(<App />, root)137 expect(root.innerHTML).toBe('<div><span>hello</span><input type="text"></div>')138 })139122 describe('performance', () => {123 it('should be O(N) for each additional element', () => {124 const App = () => (125 <>
530 unmodified lines
656 render(<App />, root)657 expect(root.innerHTML).toBe('<div><span>1</span><span>2</span></div>')658 })659660 it('empty array and non-empty array', async () => {661 const App = () => (662 <div>663 {[]}664 {[<span>1</span>]}665 </div>666 )667 render(<App />, root)668 expect(root.innerHTML).toBe('<div><span>1</span></div>')669 })670671 it('nested array', async () => {672 const nestedChildren: Child = [[[<span>1</span>], <span>2</span>]]673 const App = () => <div>{nestedChildren}</div>674 render(<App />, root)675 expect(root.innerHTML).toBe('<div><span>1</span><span>2</span></div>')676 })677678 it('sparse array with nested child', async () => {679 const sparseChildren: Child[] = []680 sparseChildren[1] = [<span>1</span>]681 const App = () => <div>{sparseChildren}</div>682 render(<App />, root)683 expect(root.innerHTML).toBe('<div><span>1</span></div>')684 })685686 it('toggle empty array and non-empty array on update', async () => {687 let setVisible: (value: boolean) => void = () => {}688 const App = () => {689 const [visible, _setVisible] = useState(false)690 setVisible = _setVisible691 return (692 <div>693 {visible ? [] : [<span key='a'>A</span>]}694 {visible ? [<span key='b'>B</span>] : []}695 </div>696 )697 }698 render(<App />, root)699 expect(root.innerHTML).toBe('<div><span>A</span></div>')700701 setVisible(true)702 await Promise.resolve()703 expect(root.innerHTML).toBe('<div><span>B</span></div>')704705 setVisible(false)706 await Promise.resolve()707 expect(root.innerHTML).toBe('<div><span>A</span></div>')708 })709710 it('reshape nested array on update', async () => {711 let setPattern: (value: number) => void = () => {}712 const App = () => {713 const [pattern, _setPattern] = useState(0)714 setPattern = _setPattern715 const children: Child =716 pattern === 0717 ? [[<span key='a'>A</span>], <span key='b'>B</span>]718 : [<span key='a'>A</span>, [<span key='b'>B</span>]]719 return <div>{children}</div>720 }721 render(<App />, root)722 expect(root.innerHTML).toBe('<div><span>A</span><span>B</span></div>')723724 setPattern(1)725 await Promise.resolve()726 expect(root.innerHTML).toBe('<div><span>A</span><span>B</span></div>')727728 setPattern(0)729 await Promise.resolve()730 expect(root.innerHTML).toBe('<div><span>A</span><span>B</span></div>')731 })732733 it('use the same children multiple times', async () => {734 const MultiChildren = ({ children }: { children: Child }) => (735 <>736 {children}
1997 unmodified lines
modified:src/jsx/dom/render.ts+1 added-1 removed
484 unmodified lines
485 const vChildren: Node[] = []486 let prevNode: Node | undefined487 for (let i = 0; i < children.length; i++) {488 if (Array.isArray(children[i])) {489 children.splice(i, 1, ...(children[i] as Child[]).flat())489 children.splice(i, 1, ...((children[i] as unknown[]).flat(Infinity) as Child[]))490 i--491 continue492 }493 let child = buildNode(children[i])
299 unmodified lines