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

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

merged 4 commits into from
Mar 25, 2021

Conversation

HcySunYang
Copy link
Member

Fix: #3471
We should cache the return value from the default factory to avoid unnecessary watcher triggering, which is what Vue2 does.

@HcySunYang HcySunYang added the need test The PR has missing test cases. label Mar 24, 2021
@posva
Copy link
Member

posva commented Mar 24, 2021

If we save the cache on the prop definition object, wouldn't it fail with two instances of the same component (as both would use the same reference to the same object)? It could also fail if people use the same prop object in two props (which they shouldn't), no?

Maybe it's safer to save it in the instance? It could also use a Symbol to prevent exposing it

@HcySunYang
Copy link
Member Author

@posva yes, I am aware of this and have corrected it.

@HcySunYang HcySunYang changed the title fix(runtime-core): cache the return value from the default factory to avoid unnecessary watcher trigger fix(runtime-core): cache the value returned from the default factory to avoid unnecessary watcher trigger Mar 24, 2021
@HcySunYang HcySunYang added ❗ p4-important Priority 4: this fixes bugs that violate documented behavior, or significantly improves perf. 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. and removed need test The PR has missing test cases. ❗ p4-important Priority 4: this fixes bugs that violate documented behavior, or significantly improves perf. labels Mar 24, 2021
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)

@yyx990803 yyx990803 merged commit 44166b4 into vuejs:master Mar 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Unexpected watch calling for some props with default generating value
4 participants