Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): cache the value returned from the default factory to avoid unnecessary watcher trigger #3474

Merged
merged 4 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion packages/runtime-core/__tests__/componentProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
serializeInner,
createApp,
provide,
inject
inject,
watch
} from '@vue/runtime-test'
import { render as domRender, nextTick } from 'vue'

Expand Down Expand Up @@ -403,4 +404,43 @@ describe('component props', () => {

expect(serializeInner(root)).toMatch('<div>60000000100000111</div>')
})

// #3474
test('should cache the value returned from the default factory to avoid unnecessary watcher trigger', async () => {
let count = 0
const Comp = {
props: {
foo: {
type: Object,
default: () => ({ val: 1 })
},
bar: Number
},
setup(props: any) {
watch(
() => props.foo,
() => {
count++
}
)
return () => h('h1', [props.foo.val, props.bar])
}
}

const foo = ref()
const bar = ref(0)
const app = createApp({
render: () => h(Comp, { foo: foo.value, bar: bar.value })
})

const root = nodeOps.createElement('div')
app.mount(root)
expect(serializeInner(root)).toMatch(`<h1>10</h1>`)
expect(count).toBe(0)

bar.value++
await nextTick()
expect(serializeInner(root)).toMatch(`<h1>11</h1>`)
expect(count).toBe(0)
})
})
8 changes: 8 additions & 0 deletions packages/runtime-core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,11 @@ export interface ComponentInternalInstance {
* @internal
*/
emitted: Record<string, boolean> | null
/**
* used for cache the value returned from the default factory to avoid unnecessary watcher trigger
* @internal
*/
propsDefaultValue: Data

/**
* setup related
Expand Down Expand Up @@ -440,6 +445,9 @@ export function createComponentInstance(
emit: null as any, // to be set immediately
emitted: null,

// props default value
propsDefaultValue: EMPTY_OBJ,

// state
ctx: EMPTY_OBJ,
data: EMPTY_OBJ,
Expand Down
12 changes: 9 additions & 3 deletions packages/runtime-core/src/componentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export function initProps(
const props: Data = {}
const attrs: Data = {}
def(attrs, InternalObjectKey, 1)
instance.propsDefaultValue = Object.create(null)
setFullProps(instance, rawProps, props, attrs)
// validation
if (__DEV__) {
Expand Down Expand Up @@ -317,16 +318,21 @@ function resolvePropValue(
value: unknown,
instance: ComponentInternalInstance
) {
const { propsDefaultValue } = instance
const opt = options[key]
if (opt != null) {
const hasDefault = hasOwn(opt, 'default')
// default values
if (hasDefault && value === undefined) {
const defaultValue = opt.default
if (opt.type !== Function && isFunction(defaultValue)) {
setCurrentInstance(instance)
value = defaultValue(props)
setCurrentInstance(null)
if (propsDefaultValue[key] !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also do

Suggested change
if (propsDefaultValue[key] !== undefined) {
if (key in propsDefaultValue) {

It's shorter which probably doesn't matter here but it also avoids the else statement.

It's also 10% faster in Edge, Chrome and Firefox but 90% slower on Safari 😆 . So not sure if my suggestion is worth applying (https://jsbench.me/u7kmn8d67h/1)

value = propsDefaultValue[key]
} else {
setCurrentInstance(instance)
value = propsDefaultValue[key] = defaultValue(props)
setCurrentInstance(null)
}
} else {
value = defaultValue
}
Expand Down