Skip to content
Merged
Prev Previous commit
Next Next commit
Switch loops to improve performance
  • Loading branch information
camdecoster committed Oct 13, 2025
commit 20c534fe7c4c039dab35c824dca3492450501973
11 changes: 7 additions & 4 deletions src/plot_api/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,19 +520,22 @@ const hasCollectionChanged = (oldCollection, newCollection) => {
if ([oldCollection, newCollection].every((a) => Array.isArray(a))) {
if (oldCollection.length !== newCollection.length) return true;

return oldCollection.some((oldVal, i) => {
for (let i = 0; i < oldCollection.length; i++) {
const oldVal = oldCollection[i];
const newVal = newCollection[i];
if (oldVal !== newVal) return isArrayOrObject(oldVal, newVal) ? hasCollectionChanged(oldVal, newVal) : true;
});
}
} else {
if (Object.keys(oldCollection).length !== Object.keys(newCollection).length) return true;

return Object.keys(oldCollection).some((k) => {
for (const k in oldCollection) {
if (k.startsWith('_')) return false;
Copy link
Contributor

Choose a reason for hiding this comment

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

this seems wrong, am I missing something? Is it supposed to be if (k.startsWith('_')) continue; ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Wait, looking closely this whole loop is problematic; it should only ever return true from within the loop. Line 535 will return false sometimes and short-circuit the whole loop if hasCollectionChanged(oldVal, newVal) returns false.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! I did a poor job of converting this loop from it's previous iteration. I updated it to only return when true.

const oldVal = oldCollection[k];
const newVal = newCollection[k];
if (oldVal !== newVal) return isArrayOrObject(oldVal, newVal) ? hasCollectionChanged(oldVal, newVal) : true;
});
}
}

return false;
};
exports.hasCollectionChanged = hasCollectionChanged;