Skip to content

Commit

Permalink
perf(vue): ignore to append existed node to improve router performance
Browse files Browse the repository at this point in the history
  • Loading branch information
zoomchan-cxj committed Nov 25, 2022
1 parent bbdd9ae commit cc24c27
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/hippy-vue-next/src/runtime/node/hippy-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ export class HippyNode extends HippyEventTarget {
*/
public get lastChild(): HippyNode | null {
const len = this.childNodes.length;

return len ? this.childNodes[len - 1] : null;
}

Expand Down Expand Up @@ -155,6 +154,8 @@ export class HippyNode extends HippyEventTarget {
throw new Error('No child to append');
}

// if childNode is the same as the last child, skip appending
if (this.lastChild === child) return;
// If the node to be added has a parent node and
// the parent node is not the current node, remove it from the container first
// In the case of keep-alive, the node still exists and will be moved to the virtual container
Expand Down
4 changes: 4 additions & 0 deletions packages/hippy-vue/src/renderer/__tests__/view-node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ test('append exist child test', (t) => {
parentNode.appendChild(childNode2);
parentNode.appendChild(childNode);
t.is(parentNode.lastChild, childNode);
parentNode.appendChild(childNode);
parentNode.appendChild(childNode);
t.is(parentNode.lastChild, childNode);
t.is(parentNode.childNodes.length, 3);
});

test('findChild test', (t) => {
Expand Down
7 changes: 4 additions & 3 deletions packages/hippy-vue/src/renderer/view-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ class ViewNode {
}

get lastChild() {
return this.childNodes.length
? this.childNodes[this.childNodes.length - 1]
: null;
const len = this.childNodes.length;
return len ? this.childNodes[len - 1] : null;
}

get meta() {
Expand Down Expand Up @@ -182,6 +181,8 @@ class ViewNode {
if (childNode.parentNode && childNode.parentNode !== this) {
throw new Error('Can\'t append child, because it already has a different parent.');
}
// if childNode is the same as the last child, skip appending
if (this.lastChild === childNode) return;
// remove childNode if exist
if (childNode.isMounted) {
this.removeChild(childNode);
Expand Down

0 comments on commit cc24c27

Please sign in to comment.