Skip to content

Commit

Permalink
feat: Use O(n) algorithm on update object
Browse files Browse the repository at this point in the history
  • Loading branch information
Azurewarth0920 committed Feb 3, 2024
1 parent 54e8493 commit 814fec6
Showing 1 changed file with 3 additions and 6 deletions.
9 changes: 3 additions & 6 deletions packages/devtools-kit/src/core/component/state/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,15 @@ class RefStateEditor {
else {
// if is reactive, then it must be object
// to prevent loss reactivity, we should assign key by key
const previousKeys = Object.keys(ref)
const previousKeysSet = new Set(Object.keys(ref))
const currentKeys = Object.keys(value)
// we should check the key diffs, if previous key is the longer
// then remove the needless keys
// @TODO: performance optimization
if (previousKeys.length > currentKeys.length) {
const diffKeys = previousKeys.filter(key => !currentKeys.includes(key))
diffKeys.forEach(key => Reflect.deleteProperty(ref, key))
}
currentKeys.forEach((key) => {
Reflect.set(ref, key, Reflect.get(value, key))
previousKeysSet.delete(key)
})
previousKeysSet.forEach(key => Reflect.deleteProperty(ref, key))
}
}

Expand Down

0 comments on commit 814fec6

Please sign in to comment.