Synphonyte / leptos-use

Collection of essential Leptos utilities inspired by React-Use / VueUse / SolidJS-USE
https://leptos-use.rs/
Apache License 2.0
309 stars 67 forks source link

Prefer signals over memos with first unused argument when possible #76

Closed mondeja closed 7 months ago

mondeja commented 7 months ago

Use impl Fn() -> T + 'static instead of impl Fn(Option<&T>) -> T + 'static when possible.

maccesch commented 7 months ago

Hey thanks for this. I don't understand the motivation behind this change though. What's the reasoning here?

mondeja commented 7 months ago

See the documentation about Memos in Leptos: https://docs.rs/leptos_reactive/0.6.5/leptos_reactive/signal_prelude/prelude/struct.Memo.html

Memos have a certain overhead compared to derived signals. In most cases, you should create a derived signal. But if the derivation calculation is expensive, you should create a memo.

maccesch commented 7 months ago

😄 I know about this documentation. But if you read carefully you see that there are cases where memos are preferred.

I think you're definitely correct about changing to derived signals in the sorting functions I'm not so sure about the other ones. While thinking about it I'm actually considering changing other "from" conversions to memos as well because usually the stuff that depends on these reactive elements is rather expensive. Although I'm not sure how that compares to the memo overhead.

What do you think?