S
SolidJS•2y ago
Ziltoid

Derived Signals vs Memos

Noob here; I'm going through the intro tutorial and I want to make sure I fully understand derived signals and memos. It seems like there are only upsides to memos over derived signals. (I am thinking along the lines of, if memos are only superior, why use derived signals at all?) So I have some questions: 1. What are the downsides of memos compared to derived signals? 2. Where would I want to use a derived signal instead of a memo?
4 Replies
lxsmnsyc
lxsmnsyc•2y ago
I guess we need to differentiate first derived vs memo 1. derived signals are just plain functions, createMemo has some other behavior 2. derived signals are "lazy", createMemo is "eager" (this will probably change in the future) 3. derived signal doesn't actually produce a signal, createMemo does so now with your question: 1. createMemo is expensive in comparison with derived signals, like if you think about it, createMemo is a subset of derived signals. 2. in almost every scenario, heck, the compiler utilizes that fact. Only use createMemo when you want to be efficient in subscriber-based logic
Ziltoid
ZiltoidOP•2y ago
Thanks for the quick reply 🙂 I think I see, that derived signals are preferable in most cases due to the lack of overhead introduced by caching with memos. Memos are only used when the benefit of caching outweighs the extra overhead introduced. It seems like this would usually come down to how many different things read the [ derived signal or memo ] (let's call it function), and/or how expensive the function is. I'm not quite sure what the implications of lazy vs eager are. Does it have to do with for example what's on screen vs not? i.e. if our function is used only in something not on the screen, a lazy derived signal is not run. But if it was instead a memo it would run despite not impacting anything. Is the above correct?
createMemo is a subset of derived signals.
I was actually thinking of it the other way, that a memo is a superset of a derived signal. i.e. memos are a derived signal that also has caching behavior on top. Maybe we are both correct depending on the way we look at it and it's not important. But let me know if I'm misunderstanding something.
lxsmnsyc
lxsmnsyc•2y ago
the "lazy" vs "eager" thing is how both derived signals and memos are evaluated. derived signals are evaluated on call, hence being "lazy". memos on the other hand are eager: they are called on construction. I mentioned "it will probably change" because lazy memos are a thing in other reactive systems
Alex Lohr
Alex Lohr•2y ago
I think one important thing are the use cases: derived signals are basically what selectors in the flux pattern are, while memos are reactivity filters. Consider the following example:
const derivedSum = () => x() + y();
derivedSum() // triggers on every change of x/y
const memoizedSum = createMemo(derivedSum);
memoizedSum() // triggers only if the result changes
const derivedSum = () => x() + y();
derivedSum() // triggers on every change of x/y
const memoizedSum = createMemo(derivedSum);
memoizedSum() // triggers only if the result changes
Want results from more Discord servers?
Add your server