From 354b265f83f12ae422c45def557d27141d37ae19 Mon Sep 17 00:00:00 2001 From: jdecroock Date: Sun, 13 Oct 2024 12:21:12 +0200 Subject: [PATCH] Optimize renders --- src/diff/index.js | 9 ++++--- test/browser/fragments.test.js | 45 ++++++++++++++-------------------- test/browser/keys.test.js | 18 ++++---------- test/browser/render.test.js | 9 +++---- 4 files changed, 31 insertions(+), 50 deletions(-) diff --git a/src/diff/index.js b/src/diff/index.js index 4b87fce7fe..a40d8dfaa5 100644 --- a/src/diff/index.js +++ b/src/diff/index.js @@ -5,7 +5,7 @@ import { RESET_MODE } from '../constants'; import { BaseComponent, getDomSibling } from '../component'; -import { Fragment } from '../create-element'; +import { Fragment, createVNode } from '../create-element'; import { diffChildren } from './children'; import { setProperty } from './props'; import { assign, isArray, removeNode, slice } from '../util'; @@ -510,12 +510,13 @@ function diffElementNodes( // @ts-expect-error dom.textContent = newChildren; + const vnode = createVNode(null, newChildren, null, null); + newVNode._children = [vnode]; + // @ts-expect-error + vnode._dom = dom.firstChild; } } else { if (oldHtml) dom.innerHTML = ''; - if (typeof oldProps.children === 'string') { - dom.removeChild(dom.firstChild); - } diffChildren( dom, diff --git a/test/browser/fragments.test.js b/test/browser/fragments.test.js index ed788c1a01..128f2801a1 100644 --- a/test/browser/fragments.test.js +++ b/test/browser/fragments.test.js @@ -774,9 +774,9 @@ describe('Fragment', () => { expect(scratch.innerHTML).to.equal(htmlForFalse); expectDomLogToBe( [ - '
fooHellobeep.insertBefore(
beep,
foo)', - '
beepbarHello.appendChild(
bar)', - '
beepHellobar.insertBefore(
Hello,
bar)' + '
barHellobeep.insertBefore(
bar,
beep)', + '
Hellobarbeep.appendChild(
Hello)', + '
barbeepHello.appendChild(
bar)' ], 'rendering true to false' ); @@ -788,8 +788,8 @@ describe('Fragment', () => { expect(scratch.innerHTML).to.equal(htmlForTrue); expectDomLogToBe( [ - '
beepHellofoo.insertBefore(
foo,
beep)', - '
foobeepHello.insertBefore(
Hello,
beep)' + '
beepHellofoo.appendChild(
Hello)', + '
boopfooHello.appendChild(
boop)' ], 'rendering false to true' ); @@ -1351,9 +1351,7 @@ describe('Fragment', () => { ); expectDomLogToBe([ '
    012345.insertBefore(
  1. 4,
  2. 0)', - '
      401235.insertBefore(
    1. 5,
    2. 0)', - '
        450123.insertBefore(
      1. 4,
      2. 0)', - '
          540123.insertBefore(
        1. 5,
        2. 0)' + '
            401235.insertBefore(
          1. 5,
          2. 0)' ]); clearLog(); @@ -1547,9 +1545,9 @@ describe('Fragment', () => { expectDomLogToBe( [ '
            boop.remove()', - '
            fooHellobeep.insertBefore(
            beep,
            foo)', - '
            beepbarHello.appendChild(
            bar)', - '
            beepHellobar.insertBefore(
            Hello,
            bar)' + '
            barHellobeep.insertBefore(
            bar,
            beep)', + '
            Hellobarbeep.appendChild(
            Hello)', + '
            barbeepHello.appendChild(
            bar)' ], 'rendering from true to false' ); @@ -1564,8 +1562,8 @@ describe('Fragment', () => { ); expectDomLogToBe( [ - '
            beepHellofoo.insertBefore(
            foo,
            beep)', - '
            foobeepHello.insertBefore(
            Hello,
            beep)', + '
            beepHellofoo.appendChild(
            Hello)', + '
            boopfooHello.appendChild(
            boop)', '
            fooHelloboop.appendChild(
            boop)' ], 'rendering from false to true' @@ -1632,11 +1630,9 @@ describe('Fragment', () => { ); expectDomLogToBe( [ - '
            fooHellobeepboopboop.insertBefore(
            beep,
            foo)', - '
            beepfooHellobeepboop.insertBefore(
            beep,
            foo)', - '
            beepbeepfooHellobeep.insertBefore(
            beep,
            foo)', - '
            beepbeepbeepbarHello.appendChild(
            bar)', - '
            beepbeepbeepHellobar.insertBefore(
            Hello,
            bar)' + '
            barHellobeepbeepbeep.insertBefore(
            bar,
            beep)', + '
            Hellobarbeepbeepbeep.appendChild(
            Hello)', + '
            barbeepbeepbeepHello.appendChild(
            bar)' ], 'rendering from true to false' ); @@ -1651,10 +1647,9 @@ describe('Fragment', () => { ); expectDomLogToBe( [ - '
            beepbeepbeepHellofoo.insertBefore(
            foo,
            beep)', - '
            foobeepbeepbeepHello.insertBefore(
            Hello,
            beep)', - '
            fooHellobeepbeepbeep.insertBefore(
            foo,
            beep)', - '
            Hellofoobeepbeepbeep.insertBefore(
            Hello,
            beep)' + '
            beepbeepbeepHellofoo.appendChild(
            Hello)', + '
            beepbeepbeepfooHello.insertBefore(
            foo,
            beep)', + '
            foobeepbeepbeepHello.insertBefore(
            Hello,
            beep)' ], 'rendering from false to true' ); @@ -3061,10 +3056,6 @@ describe('Fragment', () => { rerender(); expect(scratch.innerHTML).to.equal([div('A'), div(1), div(2)].join('')); - expectDomLogToBe([ - '
            B.remove()', - '
            1A2.insertBefore(
            A,
            1)', - '
            A11.insertBefore(
            1,
            1)' - ]); + expectDomLogToBe(['
            B.remove()', '
            2A1.appendChild(
            2)']); }); }); diff --git a/test/browser/keys.test.js b/test/browser/keys.test.js index 178c90e2f2..b83f895777 100644 --- a/test/browser/keys.test.js +++ b/test/browser/keys.test.js @@ -318,7 +318,7 @@ describe('keys', () => { render(, scratch); expect(scratch.textContent).to.equal('ba'); - expect(getLog()).to.deep.equal(['
              ab.insertBefore(
            1. b,
            2. a)']); + expect(getLog()).to.deep.equal(['
                ab.appendChild(
              1. a)']); }); it('should swap existing keyed children in the middle of a list efficiently', () => { @@ -334,7 +334,7 @@ describe('keys', () => { render(, scratch); expect(scratch.textContent).to.equal('acbd', 'initial swap'); expect(getLog()).to.deep.equal( - ['
                  abcd.insertBefore(
                1. c,
                2. b)'], + ['
                    abcd.insertBefore(
                  1. b,
                  2. d)'], 'initial swap' ); @@ -363,11 +363,7 @@ describe('keys', () => { render(, scratch); expect(scratch.textContent).to.equal('bcda', 'move to end'); expect(getLog()).to.deep.equal( - [ - '
                      abcd.insertBefore(
                    1. b,
                    2. a)', - '
                        bacd.insertBefore(
                      1. c,
                      2. a)', - '
                          bcad.insertBefore(
                        1. d,
                        2. a)' - ], + ['
                            abcd.appendChild(
                          1. a)'], 'move to end' ); @@ -409,11 +405,7 @@ describe('keys', () => { render(, scratch); expect(scratch.textContent).to.equal('acdebf'); - expect(getLog()).to.deep.equal([ - '
                              abcdef.insertBefore(
                            1. c,
                            2. b)', - '
                                acbdef.insertBefore(
                              1. d,
                              2. b)', - '
                                  acdbef.insertBefore(
                                1. e,
                                2. b)' - ]); + expect(getLog()).to.deep.equal(['
                                    abcdef.insertBefore(
                                  1. b,
                                  2. f)']); }); it('should reverse keyed children effectively', () => { @@ -438,7 +430,7 @@ describe('keys', () => { '
                                      jihgfabcde.insertBefore(
                                    1. e,
                                    2. a)', '
                                        jihgfeabcd.insertBefore(
                                      1. d,
                                      2. a)', '
                                          jihgfedabc.insertBefore(
                                        1. c,
                                        2. a)', - '
                                            jihgfedcab.insertBefore(
                                          1. b,
                                          2. a)' + '
                                              jihgfedcab.appendChild(
                                            1. a)' ]); }); diff --git a/test/browser/render.test.js b/test/browser/render.test.js index 2facdb9d2c..e1df289d48 100644 --- a/test/browser/render.test.js +++ b/test/browser/render.test.js @@ -1662,12 +1662,9 @@ describe('render()', () => { `
                                              ${b.map(n => `
                                              ${n}
                                              `).join('')}
                                              ` ); expect(getLog()).to.deep.equal([ - '
                                              0123456.insertBefore(
                                              1,
                                              0)', - '
                                              1023456.insertBefore(
                                              3,
                                              0)', - '
                                              1302456.insertBefore(
                                              5,
                                              0)', - '
                                              1350246.insertBefore(
                                              2,
                                              0)', - '
                                              1352046.insertBefore(
                                              6,
                                              0)', - '
                                              1352604.insertBefore(
                                              4,
                                              0)' + '
                                              0123456.insertBefore(
                                              2,
                                              6)', + '
                                              0134526.appendChild(
                                              4)', + '
                                              0135264.appendChild(
                                              0)' ]); clearLog();