Skip to content

Commit 8317dce

Browse files
committed
perf: use replace children for truncating nodes
1 parent d368215 commit 8317dce

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

packages/qwik/src/core/client/vnode-diff.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
dangerouslySetInnerHTML,
3333
} from '../shared/utils/markers';
3434
import { isPromise } from '../shared/utils/promises';
35-
import { type ValueOrPromise } from '../shared/utils/types';
35+
import { isArray, type ValueOrPromise } from '../shared/utils/types';
3636
import {
3737
getEventNameFromJsxEvent,
3838
getEventNameScopeFromJsxEvent,
@@ -308,7 +308,7 @@ export const vnode_diff = (
308308
* is an array produced by the `map` function.
309309
*/
310310
function descend(children: JSXChildren, descendVNode: boolean) {
311-
if (children == null) {
311+
if (children == null || (isArray(children) && children.length === 0)) {
312312
expectNoChildren();
313313
return;
314314
}

packages/qwik/src/core/client/vnode.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,9 @@ export const enum VNodeJournalOpCode {
183183
SetText = 1, // ------ [SetAttribute, target, text]
184184
SetAttribute = 2, // - [SetAttribute, target, ...(key, values)]]
185185
HoistStyles = 3, // -- [HoistStyles, document]
186-
Remove = 4, // ------- [Insert, target(parent), ...nodes]
187-
Insert = 5, // ------- [Insert, target(parent), reference, ...nodes]
186+
Remove = 4, // ------- [Remove, target(parent), ...nodes]
187+
RemoveAll = 5, // ------- [RemoveAll, target(parent)]
188+
Insert = 6, // ------- [Insert, target(parent), reference, ...nodes]
188189
}
189190

190191
export type VNodeJournal = Array<
@@ -968,6 +969,15 @@ export const vnode_applyJournal = (journal: VNodeJournal) => {
968969
idx++;
969970
}
970971
break;
972+
case VNodeJournalOpCode.RemoveAll:
973+
const removeAllParent = journal[idx++] as Element;
974+
if (removeAllParent.replaceChildren) {
975+
removeAllParent.replaceChildren();
976+
} else {
977+
// fallback if replaceChildren is not supported
978+
removeAllParent.textContent = '';
979+
}
980+
break;
971981
case VNodeJournalOpCode.Insert:
972982
const insertParent = journal[idx++] as Element;
973983
const insertBefore = journal[idx++] as Element | Text | null;
@@ -1220,8 +1230,7 @@ export const vnode_truncate = (
12201230
) => {
12211231
assertDefined(vDelete, 'Missing vDelete.');
12221232
const parent = vnode_getDomParent(vParent);
1223-
const children = vnode_getDOMChildNodes(journal, vDelete);
1224-
parent && children.length && journal.push(VNodeJournalOpCode.Remove, parent, ...children);
1233+
parent && journal.push(VNodeJournalOpCode.RemoveAll, parent);
12251234
const vPrevious = vDelete.previousSibling;
12261235
if (vPrevious) {
12271236
vPrevious.nextSibling = null;

0 commit comments

Comments
 (0)