Skip to content

Commit 41512f2

Browse files
authored
Skip startDom tree traversal when mounting new trees
1 parent 5f74915 commit 41512f2

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/diff/mount.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,10 @@ function mountDOMElement(dom, internal, globalContext, commitQueue) {
138138
}
139139
}
140140

141+
let isNew = dom == null;
142+
141143
if (flags & TYPE_TEXT) {
142-
if (dom == null) {
144+
if (isNew) {
143145
// @ts-ignore createTextNode returns Text, we expect PreactElement
144146
dom = document.createTextNode(newProps);
145147
} else if (dom.data !== newProps) {
@@ -151,7 +153,7 @@ function mountDOMElement(dom, internal, globalContext, commitQueue) {
151153
// Tracks entering and exiting SVG namespace when descending through the tree.
152154
// if (nodeType === 'svg') internal._flags |= MODE_SVG;
153155

154-
if (dom == null) {
156+
if (isNew) {
155157
if (isSvg) {
156158
dom = document.createElementNS(
157159
'http://www.w3.org/2000/svg',
@@ -167,7 +169,7 @@ function mountDOMElement(dom, internal, globalContext, commitQueue) {
167169
}
168170

169171
// we are creating a new node, so we can assume this is a new subtree (in case we are hydrating), this deopts the hydrate
170-
internal._flags &= RESET_MODE;
172+
internal._flags = flags &= RESET_MODE;
171173
isFullRender = 1;
172174
}
173175

@@ -218,7 +220,7 @@ function mountDOMElement(dom, internal, globalContext, commitQueue) {
218220
internal,
219221
globalContext,
220222
commitQueue,
221-
dom.firstChild
223+
isNew ? null : dom.firstChild
222224
);
223225
}
224226

@@ -229,7 +231,7 @@ function mountDOMElement(dom, internal, globalContext, commitQueue) {
229231
}
230232

231233
// @ts-ignore
232-
return dom.nextSibling;
234+
return isNew ? null : dom.nextSibling;
233235
}
234236

235237
/**
@@ -251,21 +253,25 @@ export function mountChildren(
251253
commitQueue,
252254
startDom
253255
) {
254-
let i, childVNode, childInternal, newDom, mountedNextChild;
256+
let internalChildren = (parentInternal._children = []),
257+
i,
258+
childVNode,
259+
childInternal,
260+
newDom,
261+
mountedNextChild;
255262

256-
parentInternal._children = [];
257263
for (i = 0; i < renderResult.length; i++) {
258264
childVNode = normalizeToVNode(renderResult[i]);
259265

260266
// Terser removes the `continue` here and wraps the loop body
261267
// in a `if (childVNode) { ... } condition
262268
if (childVNode == null) {
263-
parentInternal._children[i] = null;
269+
internalChildren[i] = null;
264270
continue;
265271
}
266272

267273
childInternal = createInternal(childVNode, parentInternal);
268-
parentInternal._children[i] = childInternal;
274+
internalChildren[i] = childInternal;
269275

270276
// Morph the old element into the new one, but don't append it to the dom yet
271277
mountedNextChild = mount(

0 commit comments

Comments
 (0)