Skip to content

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

Open
@hpeebles

Description

@hpeebles

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions