Skip to content

Commit cdee65a

Browse files
committed
perf: revert to _isRef for perf
Benchmarking shows checking for a plain property is about 4~5x faster than checking for a Symbol, likely because the Symbol does not fit well into V8's hidden class model.
1 parent 6c80e13 commit cdee65a

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

packages/reactivity/src/computed.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { effect, ReactiveEffect, activeReactiveEffectStack } from './effect'
2-
import { Ref, refSymbol, UnwrapRef } from './ref'
2+
import { Ref, UnwrapRef } from './ref'
33
import { isFunction, NOOP } from '@vue/shared'
44

55
export interface ComputedRef<T> extends WritableComputedRef<T> {
@@ -46,7 +46,7 @@ export function computed<T>(
4646
}
4747
})
4848
return {
49-
[refSymbol]: true,
49+
_isRef: true,
5050
// expose effect so computed can be stopped
5151
effect: runner,
5252
get value() {

packages/reactivity/src/ref.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import { isObject } from '@vue/shared'
44
import { reactive } from './reactive'
55
import { ComputedRef } from './computed'
66

7-
export const refSymbol = Symbol(__DEV__ ? 'refSymbol' : '')
8-
97
export interface Ref<T = any> {
10-
[refSymbol]: true
8+
_isRef: true
119
value: UnwrapRef<T>
1210
}
1311

@@ -21,7 +19,7 @@ export function ref(raw: any) {
2119
}
2220
raw = convert(raw)
2321
const v = {
24-
[refSymbol]: true,
22+
_isRef: true,
2523
get value() {
2624
track(v, OperationTypes.GET, '')
2725
return raw
@@ -35,7 +33,7 @@ export function ref(raw: any) {
3533
}
3634

3735
export function isRef(v: any): v is Ref {
38-
return v ? v[refSymbol] === true : false
36+
return v ? v._isRef === true : false
3937
}
4038

4139
export function toRefs<T extends object>(
@@ -53,7 +51,7 @@ function toProxyRef<T extends object, K extends keyof T>(
5351
key: K
5452
): Ref<T[K]> {
5553
return {
56-
[refSymbol]: true,
54+
_isRef: true,
5755
get value(): any {
5856
return object[key]
5957
},

0 commit comments

Comments
 (0)