Skip to content

Trying out moveBefore instead of insertBefore #12

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions cjs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ module.exports = (parentNode, a, b, get, before) => {
if (aEnd === aStart) {
// we could be in a situation where the rest of nodes that
// need to be added are not at the end, and in such case
// the node to `insertBefore`, if the index is more than 0
// the node to `moveBefore`, if the index is more than 0
// must be retrieved, otherwise it's gonna be the first item.
const node = bEnd < bLength ?
(bStart ?
(get(b[bStart - 1], -0).nextSibling) :
get(b[bEnd - bStart], 0)) :
before;
while (bStart < bEnd)
parentNode.insertBefore(get(b[bStart++], 1), node);
parentNode.moveBefore(get(b[bStart++], 1), node);
}
// remove head or tail: fast path
else if (bEnd === bStart) {
Expand Down Expand Up @@ -81,11 +81,11 @@ module.exports = (parentNode, a, b, get, before) => {
// [1, 2, 3, 4, 5]
// [1, 2, 3, 5, 6, 4]
const node = get(a[--aEnd], -1).nextSibling;
parentNode.insertBefore(
parentNode.moveBefore(
get(b[bStart++], 1),
get(a[aStart++], -1).nextSibling
);
parentNode.insertBefore(get(b[--bEnd], 1), node);
parentNode.moveBefore(get(b[--bEnd], 1), node);
// mark the future index as identical (yeah, it's dirty, but cheap 👍)
// The main reason to do this, is that when a[aEnd] will be reached,
// the loop will likely be on the fast path, as identical to b[bEnd].
Expand Down Expand Up @@ -131,7 +131,7 @@ module.exports = (parentNode, a, b, get, before) => {
if (sequence > (index - bStart)) {
const node = get(a[aStart], 0);
while (bStart < index)
parentNode.insertBefore(get(b[bStart++], 1), node);
parentNode.moveBefore(get(b[bStart++], 1), node);
}
// if the effort wasn't good enough, fallback to a replace,
// moving both source and target indexes forward, hoping that some
Expand Down
10 changes: 5 additions & 5 deletions esm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ export default (parentNode, a, b, get, before) => {
if (aEnd === aStart) {
// we could be in a situation where the rest of nodes that
// need to be added are not at the end, and in such case
// the node to `insertBefore`, if the index is more than 0
// the node to `moveBefore`, if the index is more than 0
// must be retrieved, otherwise it's gonna be the first item.
const node = bEnd < bLength ?
(bStart ?
(get(b[bStart - 1], -0).nextSibling) :
get(b[bEnd - bStart], 0)) :
before;
while (bStart < bEnd)
parentNode.insertBefore(get(b[bStart++], 1), node);
parentNode.moveBefore(get(b[bStart++], 1), node);
}
// remove head or tail: fast path
else if (bEnd === bStart) {
Expand Down Expand Up @@ -80,11 +80,11 @@ export default (parentNode, a, b, get, before) => {
// [1, 2, 3, 4, 5]
// [1, 2, 3, 5, 6, 4]
const node = get(a[--aEnd], -1).nextSibling;
parentNode.insertBefore(
parentNode.moveBefore(
get(b[bStart++], 1),
get(a[aStart++], -1).nextSibling
);
parentNode.insertBefore(get(b[--bEnd], 1), node);
parentNode.moveBefore(get(b[--bEnd], 1), node);
// mark the future index as identical (yeah, it's dirty, but cheap 👍)
// The main reason to do this, is that when a[aEnd] will be reached,
// the loop will likely be on the fast path, as identical to b[bEnd].
Expand Down Expand Up @@ -130,7 +130,7 @@ export default (parentNode, a, b, get, before) => {
if (sequence > (index - bStart)) {
const node = get(a[aStart], 0);
while (bStart < index)
parentNode.insertBefore(get(b[bStart++], 1), node);
parentNode.moveBefore(get(b[bStart++], 1), node);
}
// if the effort wasn't good enough, fallback to a replace,
// moving both source and target indexes forward, hoping that some
Expand Down
10 changes: 5 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ var udomdiff = (function (exports) {
if (aEnd === aStart) {
// we could be in a situation where the rest of nodes that
// need to be added are not at the end, and in such case
// the node to `insertBefore`, if the index is more than 0
// the node to `moveBefore`, if the index is more than 0
// must be retrieved, otherwise it's gonna be the first item.
var node = bEnd < bLength ? bStart ? get(b[bStart - 1], -0).nextSibling : get(b[bEnd - bStart], 0) : before;
while (bStart < bEnd) parentNode.insertBefore(get(b[bStart++], 1), node);
while (bStart < bEnd) parentNode.moveBefore(get(b[bStart++], 1), node);
}
// remove head or tail: fast path
else if (bEnd === bStart) {
Expand Down Expand Up @@ -74,8 +74,8 @@ var udomdiff = (function (exports) {
// [1, 2, 3, 4, 5]
// [1, 2, 3, 5, 6, 4]
var _node = get(a[--aEnd], -1).nextSibling;
parentNode.insertBefore(get(b[bStart++], 1), get(a[aStart++], -1).nextSibling);
parentNode.insertBefore(get(b[--bEnd], 1), _node);
parentNode.moveBefore(get(b[bStart++], 1), get(a[aStart++], -1).nextSibling);
parentNode.moveBefore(get(b[--bEnd], 1), _node);
// mark the future index as identical (yeah, it's dirty, but cheap 👍)
// The main reason to do this, is that when a[aEnd] will be reached,
// the loop will likely be on the fast path, as identical to b[bEnd].
Expand Down Expand Up @@ -118,7 +118,7 @@ var udomdiff = (function (exports) {
// will be processed at zero cost
if (sequence > index - bStart) {
var _node2 = get(a[aStart], 0);
while (bStart < index) parentNode.insertBefore(get(b[bStart++], 1), _node2);
while (bStart < index) parentNode.moveBefore(get(b[bStart++], 1), _node2);
}
// if the effort wasn't good enough, fallback to a replace,
// moving both source and target indexes forward, hoping that some
Expand Down
2 changes: 1 addition & 1 deletion min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion new.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.