Skip to content

Commit

Permalink
perf(reactivity): avoid triggering Map.has twice on non-reactive keys (
Browse files Browse the repository at this point in the history
  • Loading branch information
Picknight authored Aug 26, 2020
1 parent d5c4f6e commit 97bc30e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/reactivity/__tests__/collections/Map.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,5 +436,16 @@ describe('reactivity/collections', () => {
map.set('b', 1)
expect(spy).toHaveBeenCalledTimes(3)
})

it('should trigger has only once for non-reactive keys', () => {
const map = new Map()
const spy = jest.fn()
map.has = spy

let proxy = reactive(map)
proxy.has('k')

expect(spy).toBeCalledTimes(1)
})
})
})
4 changes: 3 additions & 1 deletion packages/reactivity/src/collectionHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ function has(this: CollectionTypes, key: unknown, isReadonly = false): boolean {
!isReadonly && track(rawTarget, TrackOpTypes.HAS, key)
}
!isReadonly && track(rawTarget, TrackOpTypes.HAS, rawKey)
return target.has(key) || target.has(rawKey)
return key === rawKey
? target.has(key)
: target.has(key) || target.has(rawKey)
}

function size(target: IterableCollections, isReadonly = false) {
Expand Down

0 comments on commit 97bc30e

Please sign in to comment.