refactor(reactivity): use more efficient reactive checks
WeakSets and WeakMaps shows degrading performance as the amount of observed objects increases. Using hidden keys result in better performance especially when repeatedly creating large amounts of reactive proxies. This also makes it possible to more efficiently declare non-reactive objects in userland.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { toRaw, reactive, readonly } from './reactive'
|
||||
import { toRaw, reactive, readonly, ReactiveFlags } from './reactive'
|
||||
import { track, trigger, ITERATE_KEY, MAP_KEY_ITERATE_KEY } from './effect'
|
||||
import { TrackOpTypes, TriggerOpTypes } from './operations'
|
||||
import {
|
||||
@@ -242,29 +242,40 @@ iteratorMethods.forEach(method => {
|
||||
)
|
||||
})
|
||||
|
||||
function createInstrumentationGetter(
|
||||
instrumentations: Record<string, Function>
|
||||
) {
|
||||
function createInstrumentationGetter(isReadonly: boolean) {
|
||||
const instrumentations = isReadonly
|
||||
? readonlyInstrumentations
|
||||
: mutableInstrumentations
|
||||
|
||||
return (
|
||||
target: CollectionTypes,
|
||||
key: string | symbol,
|
||||
receiver: CollectionTypes
|
||||
) =>
|
||||
Reflect.get(
|
||||
) => {
|
||||
if (key === ReactiveFlags.isReactive) {
|
||||
return !isReadonly
|
||||
} else if (key === ReactiveFlags.isReadonly) {
|
||||
return isReadonly
|
||||
} else if (key === ReactiveFlags.raw) {
|
||||
return target
|
||||
}
|
||||
|
||||
return Reflect.get(
|
||||
hasOwn(instrumentations, key) && key in target
|
||||
? instrumentations
|
||||
: target,
|
||||
key,
|
||||
receiver
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export const mutableCollectionHandlers: ProxyHandler<CollectionTypes> = {
|
||||
get: createInstrumentationGetter(mutableInstrumentations)
|
||||
get: createInstrumentationGetter(false)
|
||||
}
|
||||
|
||||
export const readonlyCollectionHandlers: ProxyHandler<CollectionTypes> = {
|
||||
get: createInstrumentationGetter(readonlyInstrumentations)
|
||||
get: createInstrumentationGetter(true)
|
||||
}
|
||||
|
||||
function checkIdentityKeys(
|
||||
|
||||
Reference in New Issue
Block a user