Skip to content

Commit

Permalink
Revive single text-child hot path
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Oct 3, 2024
1 parent b7f6da6 commit cd0f32b
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 158 deletions.
20 changes: 19 additions & 1 deletion src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,6 @@ function diffElementNodes(
}
}

// If the new vnode didn't have dangerouslySetInnerHTML, diff its children
if (newHtml) {
// Avoid re-applying the same '__html' if it did not changed between re-render
if (
Expand All @@ -502,8 +501,27 @@ function diffElementNodes(
}

newVNode._children = [];
} else if (typeof newChildren === 'string') {
if (newChildren !== oldProps.children) {
// Unmount any previous children
if (oldVNode._children) {
while ((i = oldVNode._children.pop())) {
// Setting textContent on the dom element will unmount all DOM nodes
// of the previous children, so we don't need to remove DOM in this
// call to unmount
unmount(i, oldVNode, true);
}
}

dom.textContent = newChildren;

Check failure on line 516 in src/diff/index.js

View workflow job for this annotation

GitHub Actions / Build & Test

Property 'textContent' does not exist on type 'PreactElement'.

Check failure on line 516 in src/diff/index.js

View workflow job for this annotation

GitHub Actions / Build & Test / Build & Test

Property 'textContent' does not exist on type 'PreactElement'.
}
} else {
if (oldHtml) dom.innerHTML = '';
// Previous render was a single text child. New children are not so let's
// unmount the previous text child
if (typeof oldProps.children === 'string') {
dom.removeChild(dom.firstChild);
}

diffChildren(
dom,
Expand Down
Loading

0 comments on commit cd0f32b

Please sign in to comment.