Replies: 3 comments 4 replies
-
I think this would be better. const __sfc__ = {
__name: 'App',
props: { a: String },
setup(__props) {
return (_ctx, _cache) => {
return (_openBlock(), _createElementBlock("div", {
title: __props.a,
+ class: _cache._hoisted_2 || (_cache._hoisted_2 = _normalizeClass({ red: ctx.foo, blue: true, green: false }))
}, null, 8 /* PROPS */, _hoisted_1))
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Another example I noticed today where this kind of optimization might kick in more often than not: ThresJs. |
Beta Was this translation helpful? Give feedback.
-
Is there a reason for not compiling it to + const _hoisted_2 = { red: true, blue: true, green: false };
const __sfc__ = {
__name: 'App',
props: { a: String },
setup(__props) {
return (_ctx, _cache) => {
return (_openBlock(), _createElementBlock("div", {
title: __props.a,
+ class: _normalizeClass(_hoisted_2)
}, null, 8 /* PROPS */, _hoisted_1))
}
}
} That would allow to use literal constants for example in (Actually in this particular case Also what about functions? It should be technically possible to detect whether function is using component's variables. |
Beta Was this translation helpful? Give feedback.
-
This is a proposed optimization to the Vue compiler
What?
I suggest that Vue compiler should detect if a bound value is a constant (i.e. an array or an object literals whose values are themselves only constants) and hoist those values into const variables.
For instance, consider this template:
Here's the current and suggested compiled render functions:
This shouldn't require a lot of work, as analyzing bindings for "const-ness" is already performed by Vue compiler to support hoisting elements. Remove the
title
binding in this example and the wholediv
is hoisted!Why?
For perf, of course.
This has two benefits:
When?
This can be more common than you'd think, for example:
motion-v
library is using complex props that are constant object literals 90% of the time.And in my experience I see almost nobody "manually" hoisting theses options into setup constants, i.e. writing:
Beta Was this translation helpful? Give feedback.
All reactions