Skip to content

Commit 82be30f

Browse files
committed
refactor: use key in instead of instanceof
key in is faster in most browsers https://jsbench.me/3qkrn7gwez/2
1 parent 3b9c143 commit 82be30f

File tree

5 files changed

+26
-31
lines changed

5 files changed

+26
-31
lines changed

packages/reactivity/__tests__/computed.spec.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
toRaw,
1010
TrackOpTypes,
1111
ITERATE_KEY,
12-
TriggerOpTypes
12+
TriggerOpTypes,
13+
isComputed
1314
} from '../src'
1415

1516
describe('reactivity/computed', () => {
@@ -272,4 +273,16 @@ describe('reactivity/computed', () => {
272273
oldValue: 2
273274
})
274275
})
276+
277+
it('isComputed', () => {
278+
expect(isComputed(undefined)).toBe(false)
279+
expect(isComputed(null)).toBe(false)
280+
expect(isComputed(3)).toBe(false)
281+
expect(isComputed({})).toBe(false)
282+
expect(isComputed(computed(() => 1))).toBe(true)
283+
expect(isComputed(ref(1))).toBe(false)
284+
expect(isComputed(reactive({}))).toBe(false)
285+
expect(isComputed(reactive({ c: computed(() => 1) }).c)).toBe(false)
286+
expect(isComputed(toRaw(reactive({ c: computed(() => 1) })).c)).toBe(true)
287+
})
275288
})

packages/reactivity/__tests__/deferredComputed.spec.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import {
2-
computed,
3-
deferredComputed,
4-
effect,
5-
isComputed,
6-
reactive,
7-
ref,
8-
toRaw
9-
} from '../src'
1+
import { computed, deferredComputed, effect, ref } from '../src'
102

113
describe('deferred computed', () => {
124
const tick = Promise.resolve()
@@ -190,13 +182,4 @@ describe('deferred computed', () => {
190182
await tick
191183
expect(c1Spy).toHaveBeenCalledTimes(1)
192184
})
193-
194-
it('isComputed', () => {
195-
expect(isComputed(computed(() => 1))).toBe(true)
196-
expect(isComputed(deferredComputed(() => 1))).toBe(true)
197-
expect(isComputed(ref(1))).toBe(false)
198-
expect(isComputed(reactive({}))).toBe(false)
199-
expect(isComputed(reactive({ c: computed(() => 1) }).c)).toBe(false)
200-
expect(isComputed(toRaw(reactive({ c: computed(() => 1) })).c)).toBe(true)
201-
})
202185
})

packages/reactivity/src/computed.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ export interface WritableComputedOptions<T> {
2020
set: ComputedSetter<T>
2121
}
2222

23-
export class ComputedRefImpl<T> {
23+
export function isComputed<T>(
24+
value: ComputedRef<T> | unknown
25+
): value is ComputedRef<T>
26+
export function isComputed(value: any): value is ComputedRef {
27+
return Boolean(value && value.effect)
28+
}
29+
30+
class ComputedRefImpl<T> {
2431
public dep?: Dep = undefined
2532

2633
private _value!: T

packages/reactivity/src/deferredComputed.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Dep } from './dep'
22
import { ReactiveEffect } from './effect'
3-
import { ComputedGetter, ComputedRef, ComputedRefImpl } from './computed'
3+
import { ComputedGetter, ComputedRef } from './computed'
44
import { ReactiveFlags, toRaw } from './reactive'
55
import { trackRefValue, triggerRefValue } from './ref'
66

@@ -24,15 +24,6 @@ const flush = () => {
2424
queued = false
2525
}
2626

27-
export function isComputed<T>(
28-
value: ComputedRef<T> | unknown
29-
): value is ComputedRef<T>
30-
export function isComputed(value: any): value is ComputedRef {
31-
return (
32-
value instanceof ComputedRefImpl || value instanceof DeferredComputedRefImpl
33-
)
34-
}
35-
3627
class DeferredComputedRefImpl<T> {
3728
public dep?: Dep = undefined
3829

packages/reactivity/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ export {
3030
} from './reactive'
3131
export {
3232
computed,
33+
isComputed,
3334
ComputedRef,
3435
WritableComputedRef,
3536
WritableComputedOptions,
3637
ComputedGetter,
3738
ComputedSetter
3839
} from './computed'
39-
export { deferredComputed, isComputed } from './deferredComputed'
40+
export { deferredComputed } from './deferredComputed'
4041
export {
4142
effect,
4243
stop,

0 commit comments

Comments
 (0)