perf(jsx/dom): update textContent only when content changes (#2264)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* perf(jsx/dom): update textContent only when content changes * chore: denoify * test: refactor test to use simple spy instead of class mock
authored by Taku Amano · committed by GitHub · parent 65597a8 · Browse files · 3 files·+49 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+49 added−10 removed
- tests:+25 added−0 removed
- other:+24 added−10 removed
Provenance
- Commit
440d31b- Tree
bfcdb5b- Author
Taku Amano <taku@taaas.jp>- Committer
GitHub <noreply@github.com>- Parent
65597a8
3 files changed+49 added−10 removed
modified:deno_dist/jsx/dom/render.ts+12 added-5 removed
46 unmodified lines47 // eslint-disable-next-line @typescript-eslint/no-explicit-any48 any[][]49 ]50 } & JSXNode51-type NodeString = [string] & {51+type NodeString = [52+ string, // text content53+ boolean // is dirty54+] & {55 e?: Text56 // like a NodeObject57 vC: undefined58 nN: undefined128 unmodified lines187 const invokeTag = (context: Context, node: NodeObject): Child[] => {188 if (node.s) {189 const res = node.s190 node.s = undefined188- return res191+ return res as Child[]192 }193 194 node[DOM_STASH][0] = 0195 buildDataStack.push([context, node])114 unmodified lines310 const child = next[i]311 312 let el: SupportedElement | Text313 if (isNodeString(child)) {311- if (child.e) {314+ if (child.e && child[1]) {315 child.e.textContent = child[0]316 }317+ child[1] = false318 el = child.e ||= document.createTextNode(child[0])319 } else {320 el = child.e ||= child.n321 ? (document.createElementNS(child.n, child.tag as string) as SVGElement | MathMLElement)57 unmodified lines379 if (isNodeString(child)) {380 if (!isNodeString(oldChild)) {381 vChildrenToRemove.push(oldChild)382 } else {383+ if (oldChild[0] !== child[0]) {379- oldChild[0] = child[0] // update text content384+ oldChild[0] = child[0] // update text content385+ oldChild[1] = true386+ }387 child = oldChild388 }389 } else if (oldChild.tag !== child.tag) {390 vChildrenToRemove.push(oldChild)40 unmodified lines431 const buildNode = (node: Child): Node | undefined => {432 if (node === undefined || node === null || typeof node === 'boolean') {433 return undefined434 } else if (typeof node === 'string' || typeof node === 'number') {428- return [node.toString()] as NodeString435+ return [node.toString(), true] as NodeString436 } else {437 if (typeof (node as JSXNode).tag === 'function') {438 ;(node as NodeObject)[DOM_STASH] = [0, []]439 } else {88 unmodified linesmodified:src/jsx/dom/index.test.tsx+25 added
242 unmodified lines243 await Promise.resolve()244 expect(root.innerHTML).toBe('<form><div><label>label</label><input></div><p>1</p></form>')245 expect(insertBeforeSpy).not.toHaveBeenCalled()246 })247+248+ it('should not call textContent for unchanged text', async () => {249+ let setCount: (count: number) => void = () => {}250+ const App = () => {251+ const [count, _setCount] = useState(0)252+ setCount = _setCount253+ return (254+ <>255+ <span>hono</span>256+ <input value={count} />257+ </>258+ )259+ }260+ render(<App />, root)261+ expect(root.innerHTML).toBe('<span>hono</span><input value="0">')262+ setCount(1)263+264+ const textContentSpy = vi.fn()265+ Object.defineProperty(dom.window.Text.prototype, 'textContent', {266+ set: textContentSpy,267+ })268+ await Promise.resolve()269+ expect(root.innerHTML).toBe('<span>hono</span><input value="1">')270+ expect(textContentSpy).not.toHaveBeenCalled()271+ })272 })273 274 describe('update properties', () => {275 describe('input', () => {1090 unmodified linesmodified:src/jsx/dom/render.ts+12 added-5 removed
46 unmodified lines47 // eslint-disable-next-line @typescript-eslint/no-explicit-any48 any[][]49 ]50 } & JSXNode51-type NodeString = [string] & {51+type NodeString = [52+ string, // text content53+ boolean // is dirty54+] & {55 e?: Text56 // like a NodeObject57 vC: undefined58 nN: undefined128 unmodified lines187 const invokeTag = (context: Context, node: NodeObject): Child[] => {188 if (node.s) {189 const res = node.s190 node.s = undefined188- return res191+ return res as Child[]192 }193 194 node[DOM_STASH][0] = 0195 buildDataStack.push([context, node])114 unmodified lines310 const child = next[i]311 312 let el: SupportedElement | Text313 if (isNodeString(child)) {311- if (child.e) {314+ if (child.e && child[1]) {315 child.e.textContent = child[0]316 }317+ child[1] = false318 el = child.e ||= document.createTextNode(child[0])319 } else {320 el = child.e ||= child.n321 ? (document.createElementNS(child.n, child.tag as string) as SVGElement | MathMLElement)57 unmodified lines379 if (isNodeString(child)) {380 if (!isNodeString(oldChild)) {381 vChildrenToRemove.push(oldChild)382 } else {383+ if (oldChild[0] !== child[0]) {379- oldChild[0] = child[0] // update text content384+ oldChild[0] = child[0] // update text content385+ oldChild[1] = true386+ }387 child = oldChild388 }389 } else if (oldChild.tag !== child.tag) {390 vChildrenToRemove.push(oldChild)40 unmodified lines431 const buildNode = (node: Child): Node | undefined => {432 if (node === undefined || node === null || typeof node === 'boolean') {433 return undefined434 } else if (typeof node === 'string' || typeof node === 'number') {428- return [node.toString()] as NodeString435+ return [node.toString(), true] as NodeString436 } else {437 if (typeof (node as JSXNode).tag === 'function') {438 ;(node as NodeObject)[DOM_STASH] = [0, []]439 } else {88 unmodified lines