From 7fe3f25791bf2f66152eb7b3465f25fae3b57bd0 Mon Sep 17 00:00:00 2001 From: MichaelWest22 Date: Thu, 19 Dec 2024 23:37:03 +1300 Subject: [PATCH] Improve persistentIdSet performance --- src/idiomorph.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/idiomorph.js b/src/idiomorph.js index 1c9dd74..385256b 100644 --- a/src/idiomorph.js +++ b/src/idiomorph.js @@ -1204,15 +1204,17 @@ var Idiomorph = (function () { * @returns {Set} the id set of all persistent nodes that exist in both old and new content */ function persistentIdSet(oldContent, newContent) { - let idSet = new Set(); + let matchIdSet = new Set(); + let oldIdSet = new Set(); for (const oldNode of nodesWithIds(oldContent)) { - for (const newNode of nodesWithIds(newContent)) { - if (oldNode.id === newNode.id) { - idSet.add(oldNode.id); - } + oldIdSet.add(oldNode.id); + } + for (const newNode of nodesWithIds(newContent)) { + if (oldIdSet.has(newNode.id)) { + matchIdSet.add(newNode.id); } } - return idSet; + return matchIdSet; } //=============================================================================