Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved hydration performance by less reordering of nodes #6392

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions src/runtime/internal/dom.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
import { has_prop } from './utils';

export function append(target: Node, node: Node) {
target.appendChild(node);
}
interface ClaimedNode extends Node {
/**
* When a node is claimed, we know all its children will be appended during mount.
* Keep track of which children have already been re-appended, so we do not reorder them multiple times.
*/
appendAt?: number;
}

export function append(target: ClaimedNode, node: Node) {
if (target.appendAt == null) {
target.appendChild(node);
} else {
const appendAt = target.appendAt;
const childNodes = target.childNodes;
const nextNode = childNodes[appendAt];

if (nextNode != node) {
target.insertBefore(node, nextNode);
}

target.appendAt = childNodes.length == appendAt + 1 ? undefined : appendAt + 1;
}
}

export function insert(target: ClaimedNode, node: Node, anchor?: Node) {
if ((target as ClaimedNode).appendAt == null) {
target.insertBefore(node, anchor || null);
} else if (anchor == null) {
append(target, node);
} else {
const appendAt = target.appendAt;
const anchorIndex = Array.prototype.indexOf.call(target.childNodes, anchor);

if (anchorIndex < appendAt) {
target.appendAt = target.childNodes.length == appendAt + 1 ? undefined : appendAt + 1;
}

export function insert(target: Node, node: Node, anchor?: Node) {
target.insertBefore(node, anchor || null);
target.insertBefore(node, anchor);
}
}

export function detach(node: Node) {
Expand Down Expand Up @@ -168,6 +201,7 @@ export function claim_element(nodes, name, attributes, svg) {
for (let k = 0; k < remove.length; k++) {
node.removeAttribute(remove[k]);
}
(node as ClaimedNode).appendAt = 0;
return nodes.splice(i, 1)[0];
}
}
Expand Down