Skip to content

$derived values being re-computed an exponential amount of times #15936

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

Open
hpeebles opened this issue May 16, 2025 · 2 comments
Open

$derived values being re-computed an exponential amount of times #15936

hpeebles opened this issue May 16, 2025 · 2 comments

Comments

@hpeebles
Copy link

Describe the bug

Hey!

We are building an app where we have recently been switching from Svelte stores to using Svelte5 $state and $derived values, but having done so we are encountering huge performance issues.

I raised #15934 last night, but having debugged further into the issue I have noticed that the stores are being recomputed vastly more than I'd have expected them to be.

Turns out that if you have a chain of derived values, where each derived value's dependency has updated but without causing the dependent value to change, then when you access the top of the chain the number of re-computations increases exponentially with the length of the chain.

Reproduction

https://svelte.dev/playground/hello-world?version=5.30.1#H4sIAAAAAAAAE7XTXWvbMBQG4L9yEIPaVHPXbWXgOIFCy7qLZTDaq2oXsnyciSlHRjrOGoL_-3C8LMnwCAR2Z1751SN_nI0gvUSRizsMdoUVrLRrEQK-Nn7ZtIwV4EvjCYmtdm4tpKitwyjy543gddNX-0DI3Ua3TZPFFTrus1JHHMuNJ0biKHJRRBNswzNFiuuWDFtPEFpKUtj0mWKHDBqm8CqyZkzepJN9XvZ5NZw9K9dJksJ0tisqNp6id5g5v0iUGB7J0gLKLMuU2G2kOCC3geCz5u_Z0lKiJdzsVrtDz5znmRNeKeH9qFed51UnPCPh3aiH53l4wqskvB316v_joYTrUW9xnlePe_URsb2sfYCklyxM4XoCFgr4MAF7eZn-i3lqKr1Vgvc8DGAOSkiwe7D__-1kvH9rDMa4_eyHQ3x43uPC0awP1OLP2_pdob9rF3df5vcXab_QKSqu9mNLRdkyewJPxlnzY7oJLXWzr09zeHy4h8eHT_OPxdVwz0yRkILxhUXOocXumxSsrftpqRJ5rV3E7hewKTmAlAQAAA==

Logs

Updating root value:  1
Accessing derived value
computing f...
computing e...
computing e...
computing d...
computing c...
computing b...
Derived value:  1
Updating root value:  2
Accessing derived value
computing b...
computing c...
computing d...
computing e...
computing e...
Derived value:  1
Updating root value:  3
Accessing derived value
computing b...
computing c...
computing d...
computing e...
computing e...
computing e...
Derived value:  1
Updating root value:  4
Accessing derived value
computing b...
computing c...
computing d...
computing e...
computing d...
computing e...
computing d...
computing e...
computing d...
Derived value:  1
Updating root value:  5
Accessing derived value
computing b...
computing c...
computing d...
computing c...
computing e...
computing c...
computing d...
computing c...
computing e...
computing c...
computing d...
computing c...
computing e...
computing c...
computing d...
computing c...
Derived value:  1
Updating root value:  6
Accessing derived value
computing b...
computing c...
computing b...
computing d...
computing b...
computing c...
computing b...
computing e...
computing b...
computing c...
computing b...
computing d...
computing b...
computing c...
computing b...
computing e...
computing b...
computing c...
computing b...
computing d...
computing b...
computing c...
computing b...
computing e...
computing b...
computing c...
computing b...
computing d...
computing b...
computing c...
computing b...
Derived value:  1
DONE

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

blocking an upgrade

@paoloricciuti
Copy link
Member

Did you notice that if you create the deriveds outside of the run function it doesn't re-evaluate this much? Are you sure this is a good reproduction of your solution where you create the derived and immediately update it?

@hpeebles
Copy link
Author

hpeebles commented May 16, 2025

In our case there are many derived values in a DAG across a few different classes, exposed via getters.
It's hard to tell exactly how many times each derived value in the chain is recalculated when a tip value is read a single time because there are many reads all happening within quick succession.
For now we will stick with stores because we really need the reads at the tips to be fast, and when using $derived the full dependency hierarchy is checked for any dirty values which is already too much overhead.

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