Skip to content

Commit 8ee2ec5

Browse files
merge findIdSetMatch and findSoftMatch so that we only iterate once, not twice.
1 parent 22dbecf commit 8ee2ec5

File tree

1 file changed

+31
-63
lines changed

1 file changed

+31
-63
lines changed

src/idiomorph.js

Lines changed: 31 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,32 @@ var Idiomorph = (function () {
352352
* @returns {Node | null}
353353
*/
354354
function findBestMatch(newChild, insertionPoint, endPoint, ctx) {
355-
return (
356-
findIdSetMatch(newChild, insertionPoint, endPoint, ctx) ||
357-
findSoftMatch(newChild, insertionPoint, endPoint, ctx)
358-
);
355+
const newChildHasPersistentIds = hasPersistentIdNodes(ctx, newChild);
356+
357+
let softMatch = null;
358+
let cursor = insertionPoint;
359+
while (cursor && cursor != endPoint) {
360+
// soft matching is a prerequisite for hard matching
361+
if (isSoftMatch(cursor, newChild)) {
362+
// if there is a possibility of an id match
363+
if (newChildHasPersistentIds) {
364+
if (isIdSetMatch(cursor, newChild, ctx)) {
365+
return cursor; // found a hard match, we're done!
366+
}
367+
}
368+
369+
// we haven't yet saved the soft match fallback
370+
if (!softMatch) {
371+
// the current soft match will hard match something else in the future, leave it
372+
if (!hasPersistentIdNodes(ctx, cursor)) {
373+
softMatch = cursor; // save this as the fallback if we don't find a hard match
374+
}
375+
}
376+
}
377+
cursor = cursor.nextSibling;
378+
}
379+
380+
return softMatch;
359381
}
360382

361383
/**
@@ -366,9 +388,10 @@ var Idiomorph = (function () {
366388
* @returns {boolean}
367389
*/
368390
function isIdSetMatch(oldNode, newNode, ctx) {
369-
return oldNode instanceof Element &&
370-
isSoftMatch(oldNode, newNode) &&
371-
getIdIntersectionCount(oldNode, newNode, ctx) > 0;
391+
return (
392+
oldNode instanceof Element &&
393+
getIdIntersectionCount(oldNode, newNode, ctx) > 0
394+
);
372395
}
373396

374397
/**
@@ -394,61 +417,6 @@ var Idiomorph = (function () {
394417
);
395418
}
396419

397-
/**
398-
* Scans forward from the insertionPoint in the old parent looking for a potential id match
399-
* for the newChild.
400-
* @param {Node} newChild
401-
* @param {Node | null} insertionPoint
402-
* @param {Node | null} endPoint
403-
* @param {MorphContext} ctx
404-
* @returns {Node | null}
405-
*/
406-
function findIdSetMatch(newChild, insertionPoint, endPoint, ctx) {
407-
const newChildPotentialIdCount = getPersistentIdNodeCount(
408-
ctx,
409-
newChild,
410-
);
411-
412-
// only search forward if there is a possibility of an id match
413-
if (newChildPotentialIdCount > 0) {
414-
let potentialMatch = insertionPoint;
415-
while (potentialMatch && potentialMatch != endPoint) {
416-
// If we have an id match, return the current potential match
417-
if (isIdSetMatch(potentialMatch, newChild, ctx)) {
418-
return potentialMatch;
419-
}
420-
potentialMatch = potentialMatch.nextSibling;
421-
}
422-
}
423-
return null;
424-
}
425-
426-
/**
427-
* Scans forward from the insertionPoint in the old parent looking for a potential soft match
428-
* for the newChild.
429-
* @param {Node} newChild
430-
* @param {Node | null} insertionPoint
431-
* @param {Node | null} endPoint
432-
* @param {MorphContext} ctx
433-
* @returns {null | Node}
434-
*/
435-
function findSoftMatch(newChild, insertionPoint, endPoint, ctx) {
436-
let potentialSoftMatch = insertionPoint;
437-
let nextSibling = newChild.nextSibling;
438-
439-
while (potentialSoftMatch && potentialSoftMatch != endPoint) {
440-
// the current potential soft match has a id set match with the remaining new
441-
// content so leave this one for the future
442-
if (!hasPersistentIdNodes(ctx, potentialSoftMatch)) {
443-
if (isSoftMatch(potentialSoftMatch, newChild)) {
444-
return potentialSoftMatch;
445-
}
446-
}
447-
potentialSoftMatch = potentialSoftMatch.nextSibling;
448-
}
449-
return null;
450-
}
451-
452420
return findBestMatch;
453421
})();
454422

@@ -578,7 +546,7 @@ var Idiomorph = (function () {
578546
let oldSet = ctx.idMap.get(oldNode);
579547
let newSet = ctx.idMap.get(newNode);
580548

581-
if(!newSet || !oldSet) return 0
549+
if (!newSet || !oldSet) return 0;
582550

583551
let matchCount = 0;
584552
for (const id of oldSet) {

0 commit comments

Comments
 (0)