You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I find that, when choosing between pureComputed and computed, pureComputed is almost always the right one due to laziness and performance benefits.
You really want computed only when you want immediate and predictable evaluation for side-effects.
Also you almost certainly want to break your computed in two parts with ignoreDependencies to make sure evaluation happens only when you want:
ko.computed(()=>{// register dependenciesko.ignoreDependencies(()=>{// produce side-effects});});
This is why I propose that computed is renamed to a longer (and scarier) impureComputed or autorun (like in mobx), possibly changing it's signature dividing "register dependencies" and "produce side-effects" parts. And pureComputed is renamed to computed to make it the go-to option for less experienced knockout.js developers. Of course this would break compatibility, so this is probably only feasible for the next major release.
If we could make documentation advise towards pureComputed and against computed when in doubt, that would be great.
If it were possible to ban any non-local mutation in pureComputed's read and write functions (except writing other observables in write), I'd also root for that.
The text was updated successfully, but these errors were encountered:
Thanks @futpib – I'm closing this but opening the same issue at tko.computed (referenced above), since that's where it stands a better chance of being accepted.
I'd also add that in our codebase we ban the use of ko.computed. When we actually want that behaviour I've aliased ko.sideEffects to be the same as ko.computed.
In our KO components we always have a dispose method which calls ko.disposeComputeds(this) - disposeComputeds is a simple function that shallow-enumerates the given object's properties and, if any are a computed, those computeds are disposed.
That seems to have handled all memory leaks, etc.
I find that, when choosing between
pureComputed
andcomputed
,pureComputed
is almost always the right one due to laziness and performance benefits.You really want
computed
only when you want immediate and predictable evaluation for side-effects.Also you almost certainly want to break your
computed
in two parts withignoreDependencies
to make sure evaluation happens only when you want:This is why I propose that
computed
is renamed to a longer (and scarier)impureComputed
orautorun
(like in mobx), possibly changing it's signature dividing "register dependencies" and "produce side-effects" parts. AndpureComputed
is renamed tocomputed
to make it the go-to option for less experienced knockout.js developers. Of course this would break compatibility, so this is probably only feasible for the next major release.If we could make documentation advise towards
pureComputed
and againstcomputed
when in doubt, that would be great.If it were possible to ban any non-local mutation in
pureComputed
'sread
andwrite
functions (except writing other observables inwrite
), I'd also root for that.The text was updated successfully, but these errors were encountered: