Skip to content

Performance issue with contexts using $derived in pair with array operations (dev mode only) #14491

Closed
@plgingras88

Description

@plgingras88

Describe the bug

In development mode only, Svelte contexts can experience significant performance slowdowns when used in pair with any array operations (map, filter, etc.) in a $derived, no matter if the derived dependencies have changed or not.

Steps to reproduce:

1- Create a "runes class" with 3 runes:

  • A $state rune (A) that is an array.
  • A $derived rune (B) that simply references the $state and performs a trivial operation (e.g., = array.map(item => item))).
  • A $state rune (C) that has no relationship with the previous $state (A) and its $derived (B).

2- Instantiate the class and pass the instance to a context using setContext.

3- Have multiple child components call getContext to retrieve the context.

Observed behavior:
If $state (C) changes, despite it has no impact on $state (A) neither its $derived (B), each additional child component calling getContext introduces incremental delays, which accumulate and result in a noticeable slowdown.

Reproduction

https://svelte.dev/playground/25a02d8edb9f4a4b9e5de54188beb94e?version=5.2.9

Simply hover any row. It will change the hovered $state (C). You'll notice it is laggy. But if you remove the array map in the $derived, it is fast again.

In my real-world codebase, running a devtool perf graph exposes hundreds of add_owner_to_object and add_owner instances. These seem to be only existing in dev mode.

Logs

No response

System Info

System:
    OS: Windows 11 10.0.22631
    pnpm: 9.12.1 - ~\AppData\Roaming\npm\pnpm.CMD
    bun: 1.1.29 - ~\AppData\Roaming\npm\bun.CMD
  Browsers:
    Edge: Chromium (131.0.2903.63)
    Internet Explorer: 11.0.22621.3527
  npmPackages:
    svelte: ^5.2.6 => 5.2.10

Severity

annoyance

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions