Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect opportunity to insert new vnode earlier for more efficient DOM manipulation #1106

Merged
merged 1 commit into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/init.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Module } from "./modules/module";
import { vnode, VNode } from "./vnode";
import { Key, vnode, VNode } from "./vnode";
import * as is from "./is";
import { htmlDomApi, DOMAPI } from "./htmldomapi";

Expand Down Expand Up @@ -49,7 +49,7 @@ function isDocumentFragment(
return api.isDocumentFragment!(vnode as any);
}

type KeyToIndexMap = { [key: string]: number };
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to the PR but I just happened to improve this type as well.

type KeyToIndexMap = { [key: Key]: number };

type ArraysOf<T> = {
[K in keyof T]: Array<T[K]>;
Expand All @@ -66,7 +66,7 @@ function createKeyToOldIdx(
for (let i = beginIdx; i <= endIdx; ++i) {
const key = children[i]?.key;
if (key !== undefined) {
map[key as string] = i;
map[key] = i;
}
}
return map;
Expand Down Expand Up @@ -352,15 +352,26 @@ export function init(
if (oldKeyToIdx === undefined) {
oldKeyToIdx = createKeyToOldIdx(oldCh, oldStartIdx, oldEndIdx);
}
idxInOld = oldKeyToIdx[newStartVnode.key as string];
idxInOld = oldKeyToIdx[newStartVnode.key!];
if (isUndef(idxInOld)) {
// New element
// `newStartVnode` is new, create and insert it in beginning
api.insertBefore(
parentElm,
createElm(newStartVnode, insertedVnodeQueue),
oldStartVnode.elm!
);
newStartVnode = newCh[++newStartIdx];
} else if (isUndef(oldKeyToIdx[newEndVnode.key!])) {
// `newEndVnode` is new, create and insert it in the end
api.insertBefore(
parentElm,
createElm(newEndVnode, insertedVnodeQueue),
api.nextSibling(oldEndVnode.elm!)
);
newEndVnode = newCh[--newEndIdx];
} else {
// Neither of the new endpoints are new vnodes, so we make progress by
// moving `newStartVnode` into position
elmToMove = oldCh[idxInOld];
if (elmToMove.sel !== newStartVnode.sel) {
api.insertBefore(
Expand All @@ -373,8 +384,8 @@ export function init(
oldCh[idxInOld] = undefined as any;
api.insertBefore(parentElm, elmToMove.elm!, oldStartVnode.elm!);
}
newStartVnode = newCh[++newStartIdx];
}
newStartVnode = newCh[++newStartIdx];
}
}

Expand Down
41 changes: 40 additions & 1 deletion test/unit/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import {
DestroyHook,
UpdateHook,
Key,
fragment
fragment,
DOMAPI
} from "../../src/index";

const hasSvgClassList = "classList" in SVGElement.prototype;
Expand Down Expand Up @@ -837,6 +838,44 @@ describe("snabbdom", function () {
assert.equal(elm.children[1].innerHTML, "symbol");
assert.equal(elm.children[2].innerHTML, "symbol");
});
it("simultaneous addition in beginning and removal in end", function () {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test also passed before this PR. I'm adding it for symmetry with the test below and to make sure that the non-inefficent case reported in #1099 is now tested.

let insertCount = 0;
const domApi: DOMAPI = {
...htmlDomApi,
insertBefore: (a, b, c) => {
insertCount++;
htmlDomApi.insertBefore(a, b, c);
}
};
const patch = init([], domApi);
const vnode1 = h("span", [2, 3, 4, 5].map(spanNum));
const vnode2 = h("span", [1, 2, 3, 4].map(spanNum));
elm = patch(vnode0, vnode1).elm;
assert.deepEqual(map(inner, elm.children), ["2", "3", "4", "5"]);
elm = patch(vnode1, vnode2).elm;
assert.deepEqual(map(inner, elm.children), ["1", "2", "3", "4"]);
assert.strictEqual(insertCount, 1);
});
it("simultaneous removal in beginning and addition in end", function () {
let insertCount = 0;
const domApi: DOMAPI = {
...htmlDomApi,
insertBefore: (a, b, c) => {
insertCount++;
htmlDomApi.insertBefore(a, b, c);
}
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change has no effect on the resulting DOM, only on the process by which we get there. Hence the test wraps the htmlDomApi to be able to observe what goes on in there.

const patch = init([], domApi);
const vnode1 = h("span", [1, 2, 3, 4].map(spanNum));
const vnode2 = h("span", [2, 3, 4, 5].map(spanNum));
elm = patch(vnode0, vnode1).elm;
assert.equal(elm.children.length, 4);
assert.deepEqual(map(inner, elm.children), ["1", "2", "3", "4"]);
elm = patch(vnode1, vnode2).elm;
assert.equal(elm.children.length, 4);
assert.deepEqual(map(inner, elm.children), ["2", "3", "4", "5"]);
assert.strictEqual(insertCount, 1);
});
});
it("reverses elements", function () {
const vnode1 = h("span", [1, 2, 3, 4, 5, 6, 7, 8].map(spanNum));
Expand Down