diff --git a/packages/reactivity/__tests__/collections/Map.spec.ts b/packages/reactivity/__tests__/collections/Map.spec.ts index 6ee2c81e..5219a9ea 100644 --- a/packages/reactivity/__tests__/collections/Map.spec.ts +++ b/packages/reactivity/__tests__/collections/Map.spec.ts @@ -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) + }) }) }) diff --git a/packages/reactivity/src/collectionHandlers.ts b/packages/reactivity/src/collectionHandlers.ts index 4f4b9726..24ae1515 100644 --- a/packages/reactivity/src/collectionHandlers.ts +++ b/packages/reactivity/src/collectionHandlers.ts @@ -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) {