Precise DOM morphing
morphing typescript dom

Index keyed candidates by id in child matching

Reduce repeated scans in the exact-id matching pass so keyed reorders spend less time searching candidates without changing morph behavior.

+15 -4
+15 -4
src/morphlex.ts
··· 459 459 const candidateNodeIndices: Array<number> = [] 460 460 const candidateElementIndices: Array<number> = [] 461 461 const candidateElementWithIdIndices: Array<number> = [] 462 + const candidateElementIndicesById: Map<string, Array<number>> = new Map() 462 463 const unmatchedNodeIndices: Array<number> = [] 463 464 const unmatchedElementIndices: Array<number> = [] 464 465 const whitespaceNodeIndices: Array<number> = [] ··· 484 485 if (nodeType === ELEMENT_NODE_TYPE) { 485 486 const candidateElement = candidate as Element 486 487 candidateLocalNameMap[i] = candidateElement.localName 487 - if (candidateElement.id !== "") { 488 + const candidateId = candidateElement.id 489 + if (candidateId !== "") { 488 490 candidateElementWithIdActive[i] = 1 489 491 candidateElementWithIdIndices.push(i) 492 + 493 + const existingIndices = candidateElementIndicesById.get(candidateId) 494 + if (existingIndices) { 495 + existingIndices.push(i) 496 + } else { 497 + candidateElementIndicesById.set(candidateId, [i]) 498 + } 490 499 } else { 491 500 candidateElementActive[i] = 1 492 501 candidateElementIndices.push(i) ··· 551 560 552 561 if (id === "") continue 553 562 554 - for (let c = 0; c < candidateElementWithIdIndices.length; c++) { 555 - const candidateIndex = candidateElementWithIdIndices[c]! 563 + const candidateIndices = candidateElementIndicesById.get(id) 564 + if (!candidateIndices) continue 565 + 566 + for (let c = 0; c < candidateIndices.length; c++) { 567 + const candidateIndex = candidateIndices[c]! 556 568 if (!candidateElementWithIdActive[candidateIndex]) continue 557 569 const candidate = fromChildNodes[candidateIndex] as Element 558 570 ··· 668 680 if (!unmatchedNodeActive[unmatchedIndex]) continue 669 681 670 682 const node = toChildNodes[unmatchedIndex]! 671 - 672 683 for (let c = 0; c < candidateNodeIndices.length; c++) { 673 684 const candidateIndex = candidateNodeIndices[c]! 674 685 if (!candidateNodeActive[candidateIndex]) continue