Reusing objects in derived expressions weirdness #13983
-
So I was experimenting with re-using objects in I now realize that returning the same object every time is an issue because the derived does an equality check and thinks that the value hasn't changed. However, while experimenting I hit this case that I don't understand: Why does having the second one enabled cause both to update? And on a broader level, is reusing objects in deriveds like this a potential performance improvement? Or are there unavoidable issues with this and I should just bite the bullet and allocate every frame? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
If you look at the compiled code you will find this: $.template_effect(() => {
$.set_text(text_1, `dimensions re-use: ${$.get(dimensionsReuse).width ?? ""}`);
$.set_text(text_2, `dimensions new: ${$.get(dimensionsNew).width ?? ""}`);
}); Updates are batched, it updates as a side effect of the other derived. So if you want reliable updates, you need a new object. There might be other ways, e.g. using state + effect but I don't know if they are any better. |
Beta Was this translation helpful? Give feedback.
Batching limits the number of active effects. The fine-grained reactivity is mostly relevant for
{#each}
where it prevents the entire list from being invalidated when one item changes.