Skip to content
This repository was archived by the owner on May 5, 2021. It is now read-only.

Commit 15ded8e

Browse files
committed
improve perf => _filterNodeInRoot (8ms for killer, 1.5ms short)
1 parent 0260c65 commit 15ded8e

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

packages/plugin-dom-layout/src/DomLayoutEngine.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ export class DomLayoutEngine extends LayoutEngine {
122122
this._currentlyRedrawing = true;
123123

124124
if (nodes) {
125-
nodes = nodes.filter(node => {
126-
return node.ancestors().pop() === this.root;
127-
});
125+
nodes = this._filterNodeInRoot(nodes);
128126
} else {
129127
nodes = [];
130128
// Redraw all.
@@ -221,6 +219,26 @@ export class DomLayoutEngine extends LayoutEngine {
221219
// Private
222220
//--------------------------------------------------------------------------
223221

222+
private _filterNodeInRoot(nodes: VNode[]): VNode[] {
223+
const ancestorInRoot = new Set<VNode>();
224+
const nodesToKeep: VNode[] = [];
225+
for (const node of nodes) {
226+
const parents: VNode[] = [];
227+
let ancestor = node.parent;
228+
while (ancestor) {
229+
if (ancestor === this.root || ancestorInRoot.has(ancestor)) {
230+
nodesToKeep.push(node);
231+
for (const parent of parents) {
232+
ancestorInRoot.add(parent);
233+
}
234+
break;
235+
}
236+
parents.push(ancestor);
237+
ancestor = ancestor.parent;
238+
}
239+
}
240+
return nodesToKeep;
241+
}
224242
/**
225243
* Render the given VSelection as a DOM selection in the given target.
226244
*

0 commit comments

Comments
 (0)