Skip to content

Commit f1c86e1

Browse files
committed
refactor: don't allocate new array for props diff
1 parent c4752e6 commit f1c86e1

File tree

1 file changed

+28
-21
lines changed

1 file changed

+28
-21
lines changed

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

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,20 +1288,39 @@ function propsDiffer(src: Record<string, any>, dst: Record<string, any>): boolea
12881288
if (srcEmpty || dstEmpty) {
12891289
return true;
12901290
}
1291-
let srcKeys = removePropsKeys(Object.keys(src), ['children', QBackRefs]);
1292-
let dstKeys = removePropsKeys(Object.keys(dst), ['children', QBackRefs]);
1293-
if (srcKeys.length !== dstKeys.length) {
1291+
1292+
const srcKeys = Object.keys(src);
1293+
const dstKeys = Object.keys(dst);
1294+
1295+
let srcLen = srcKeys.length;
1296+
let dstLen = dstKeys.length;
1297+
1298+
if ('children' in src) {
1299+
srcLen--;
1300+
}
1301+
if (QBackRefs in src) {
1302+
srcLen--;
1303+
}
1304+
if ('children' in dst) {
1305+
dstLen--;
1306+
}
1307+
if (QBackRefs in dst) {
1308+
dstLen--;
1309+
}
1310+
1311+
if (srcLen !== dstLen) {
12941312
return true;
12951313
}
1296-
srcKeys = srcKeys.sort();
1297-
dstKeys = dstKeys.sort();
1298-
for (let idx = 0; idx < srcKeys.length; idx++) {
1299-
const srcKey = srcKeys[idx];
1300-
const dstKey = dstKeys[idx];
1301-
if (srcKey !== dstKey || src[srcKey] !== dst[dstKey]) {
1314+
1315+
for (const key of srcKeys) {
1316+
if (key === 'children' || key === QBackRefs) {
1317+
continue;
1318+
}
1319+
if (!Object.prototype.hasOwnProperty.call(dst, key) || src[key] !== dst[key]) {
13021320
return true;
13031321
}
13041322
}
1323+
13051324
return false;
13061325
}
13071326

@@ -1312,18 +1331,6 @@ function isPropsEmpty(props: Record<string, any>): boolean {
13121331
return Object.keys(props).length === 0;
13131332
}
13141333

1315-
function removePropsKeys(keys: string[], propKeys: string[]): string[] {
1316-
for (let i = propKeys.length - 1; i >= 0; i--) {
1317-
const propKey = propKeys[i];
1318-
const propIdx = keys.indexOf(propKey);
1319-
if (propIdx !== -1) {
1320-
keys.splice(propIdx, 1);
1321-
}
1322-
}
1323-
1324-
return keys;
1325-
}
1326-
13271334
/**
13281335
* If vnode is removed, it is necessary to release all subscriptions associated with it.
13291336
*

0 commit comments

Comments
 (0)