perf(reactivity): avoid triggering Map.has twice on non-reactive keys (#1972)

This commit is contained in:
Pick 2020-08-26 23:41:18 +08:00 committed by GitHub
parent d5c4f6ed4d
commit 97bc30edad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 1 deletions

View File

@ -436,5 +436,16 @@ describe('reactivity/collections', () => {
map.set('b', 1) map.set('b', 1)
expect(spy).toHaveBeenCalledTimes(3) 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)
})
}) })
}) })

View File

@ -59,7 +59,9 @@ function has(this: CollectionTypes, key: unknown, isReadonly = false): boolean {
!isReadonly && track(rawTarget, TrackOpTypes.HAS, key) !isReadonly && track(rawTarget, TrackOpTypes.HAS, key)
} }
!isReadonly && track(rawTarget, TrackOpTypes.HAS, rawKey) !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) { function size(target: IterableCollections, isReadonly = false) {