Skip to content

Commit 362424e

Browse files
committed
minor tweaks for better perf on sporadic nodes
1 parent e9b2e76 commit 362424e

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/dom/diff.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,16 @@ export default (a, b, get, before) => {
109109
while (i < bEnd)
110110
map.set(b[i], i++);
111111
}
112-
// if it's a future node, hence it needs some handling
113-
if (map.has(a[aStart])) {
114-
// grab the index of such node, 'cause it might have been processed
115-
const index = map.get(a[aStart]);
112+
113+
const index = map.get(a[aStart]) ?? -1;
114+
115+
// this node has no meaning in the future list, so it's more than safe
116+
// to remove it, and check the next live node out instead, meaning
117+
// that only the live list index should be forwarded
118+
//@ts-ignore
119+
if (index < 0) get(a[aStart++], -1).remove();
120+
// it's a future node, hence it needs some handling
121+
else {
116122
// if it's not already processed, look on demand for the next LCS
117123
if (bStart < index && index < bEnd) {
118124
let i = aStart;
@@ -150,11 +156,6 @@ export default (a, b, get, before) => {
150156
else
151157
aStart++;
152158
}
153-
// this node has no meaning in the future list, so it's more than safe
154-
// to remove it, and check the next live node out instead, meaning
155-
// that only the live list index should be forwarded
156-
//@ts-ignore
157-
else get(a[aStart++], -1).remove();
158159
}
159160
}
160161
return b;

src/dom/update.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,28 @@ const comment_array = (node, value) => {
6464
);
6565
};
6666

67+
const text = new WeakMap;
68+
const getText = (ref, value) => {
69+
let node = text.get(ref);
70+
if (node) node.data = value;
71+
else text.set(ref, (node = document.createTextNode(value)));
72+
return node;
73+
};
74+
6775
const comment_hole = (node, value) => {
68-
const current = value == null ? node : (typeof value === 'object' ? value : document.createTextNode(value));
69-
(node[nodes] || node).replaceWith(diffFragment(current, 1));
70-
node[nodes] = current;
76+
const current = typeof value === 'object' ? (value ?? node) : getText(node, value);
77+
const prev = node[nodes] ?? node;
78+
if (current !== prev)
79+
prev.replaceWith(diffFragment(node[nodes] = current, 1));
7180
};
7281

7382
const comment_unsafe = xml => (node, value) => {
74-
comment_hole(node, PersistentFragment(fragment(value, xml)));
83+
const prev = node[ref] ?? (node[ref] = {});
84+
if (prev.v !== value) {
85+
prev.f = PersistentFragment(fragment(value, xml));
86+
prev.v = value;
87+
}
88+
comment_hole(node, prev.f);
7589
};
7690

7791
const comment_signal = (node, value) => {
@@ -103,7 +117,6 @@ const direct = name => (node, value) => {
103117
};
104118

105119
const dots = (node, values) => {
106-
const xml = 'ownerSVGElement' in node;
107120
for (const [name, value] of entries(values))
108121
attribute(name)(node, value);
109122
};

0 commit comments

Comments
 (0)