-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[Discussion] Remove changed checks for simple computed properties #415
Comments
It's an interesting thought and one definitely worth exploring. Part of the problem is that while the computation itself might be trivial, something less trivial could depend on that computation: <ComplexComponent selection='{{selection}}'/>
<script>
export default {
data: function () {
return {
items: [...],
selectedIndex: 42
};
},
computed: {
selected: function ( items, selectedIndex ) {
return items[ selectedIndex ];
}
}
};
</script> Now, say you did Without the check, FWIW, that code will get a little less verbose as of #413, which I plan to tackle this morning. |
We could move it into a helper function, but due to monomorphism it would end up being slower. I don't think there's an easy way to make the code smaller here, because it does need to check each of the properties for performance. |
@Rich-Harris It'll only be as fast as the current solution if the passed parameters to |
Hmm. Would this be a good intermediate solution? function isMutable ( thing ) {
var type = typeof thing;
return thing && type === 'object' || type === 'function';
}
if ( isInitial || ( 'foo' in newState && state.foo !== oldState.foo || isMutable( state.foo ) ) ) {
state.bar = newState.bar = template.computed.bar( state.foo );
} How much potential impact are we talking about? One thing I've idly wondered about is whether we could leverage type information to remove some of the ambiguity in cases like these. For example if we knew that if ( isInitial || 'foo' in newState ) {
state.bar = newState.bar = template.computed.bar( state.foo );
} In the case where we encounter <script>
export default {
data () {
return { foo: couldBeAnything };
},
computed: {
bar: foo => doSomethingWith( foo )
},
types: {
foo: Object
}
};
</script> Nice thing about that approach is you can remove the |
So from what I've found, with primitives it doesn't matter very much. There's definitely a slowdown with different objects, but passing a string one moment and a number the next it seems the performance isn't that bad (I'm assuming because the JS engine can cache a few different types before discarding.) Looking over the numbers, your |
As of recent versions, Svelte uses the |
I have the input,
Which gives the large output,
Would it make sense for simple computed properties to not do these checks? We could define a simple computed property as one that does not invoke any functions.
As a quick test, I removed these checks from a simple project, and it decreased the min+gz size by 5%.
The text was updated successfully, but these errors were encountered: