Skip to content

$derived value re-computed each time it is read even if no dependencies have changed #15934

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

Closed
hpeebles opened this issue May 15, 2025 · 2 comments

Comments

@hpeebles
Copy link

hpeebles commented May 15, 2025

Describe the bug

This is more of an annoyance than a bug since the correct value ends up being returned, but unnecessary computation takes place (which in my use case is causing significant performance degradations).

The issue occurs when a unowned derived value depends on some state, then that state is updated, but the derived value evaluates to the same thing based on the new state. From this point on the derived value is always flagged as MAYBE_DIRTY and so is re-computed each time it is read.

Reproduction

https://svelte.dev/playground/hello-world?version=5.30.1#H4sIAAAAAAAAE7VSTU_jMBD9K6MRUlMpBMrRpJWQQMteirQLJ8zBSSbFwh1H9rgsqvLfV1EoX9sVB8T1ffn52VtksyZUeE7BbqiBjXGJINBh7dddEmowx9Y6iqhutyhP3SAeAMx31rOuK-KGnAxYZSLtw2vPQiwRFZaxDraThWYtbeJarGcIibMpbAdMiyMBA3M4iGKEstn09BWvBrwZ2xbVU5ZNYb7YGbXUnqN3VDi_yjSOl7C8KopC4y5GSyBJgcFACbPjZ7R_oflj0OS3UAfHClobohyKXQ8TmQZ8C83b4SbTPeZ3CjXJodqn2ln_f_5MQeoaIwTBexnjcoj3PrnmzYvtbzTMeXL6je1O1DjJuzwwK2P5pSR7-bToV8r9S51fLS9GutdcHr3-PC6rJOIZPNfO1g_zbUjcL37dLOH68gKuL38uf5RHo2ahGXMU-iOoJCTq73IUY92j5QZVa1yk_i82oS0vSQMAAA==

Logs

Step 0: first-time read of derived value
computing...
derived value: true

Step 1: update root value, should re-compute derived value
computing...
derived value: true

Step 2: read derived value again, should not re-compute derived value
computing...
derived value: true

System Info

System:
    OS: macOS 15.4.1
    CPU: (14) arm64 Apple M3 Max
    Memory: 1.02 GB / 36.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.17.0 - ~/.nvm/versions/node/v20.17.0/bin/node
    Yarn: 1.22.22 - /opt/homebrew/bin/yarn
    npm: 10.8.2 - ~/.nvm/versions/node/v20.17.0/bin/npm
  Browsers:
    Chrome: 136.0.7103.114
    Safari: 18.4

Severity

annoyance

@paoloricciuti
Copy link
Member

Duplicato of #15414 not much we can do unfortunately but as you said it's just a minor inconvenience

#15414 (comment)

@hpeebles
Copy link
Author

We're having to revert back to using stores because of the performance hit caused by this.
We have derived values that are updated very infrequently but are read a high volume of times and they are being recomputed each time.
Essentially we need a way of having eagerly evaluated derived values.
We could switch the values to $state and then use $effects to update them, but the docs recommend against that.
Any ideas for best way to solve this problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants