Skip to content

Conversation

Sheraff
Copy link
Contributor

@Sheraff Sheraff commented Sep 20, 2025

Assuming Derived stores are more often updated than created/destroyed, it should be more performant to pre-compute the deps order from registerOnGraph when we add them to __storeToDerived, than to re-sort every time we call __flush_internals


benchmark (tl;dr 3.7x)

function setup() {
  const a = new Store(1)
  const b = new Derived({ deps: [a], fn: () => a.state })
  b.mount()
  const c = new Derived({ deps: [a], fn: () => a.state })
  c.mount()
  const d = new Derived({ deps: [b], fn: () => b.state })
  d.mount()
  const e = new Derived({ deps: [b], fn: () => b.state })
  e.mount()
  const f = new Derived({ deps: [c], fn: () => c.state })
  f.mount()
  const g = new Derived({
    deps: [d, e, f],
    fn: () => d.state + e.state + f.state,
  })
  g.mount()

  g.subscribe(() => noop(g.state))

  return a
}

describe('Derived', () => {
  const a = setup()
  bench('New', () => {
    for (let i = 0; i < 1000; i++)
      a.setState(() => 2)
  })

  const b = setupOld()
  bench('Old', () => {
    for (let i = 0; i < 1000; i++)
      b.setState(() => 2)
  })
})
 ✓  @tanstack/store  tests/foo.bench.ts > Derived 1218ms
     name        hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · New   1,938.60  0.4730  0.6508  0.5158  0.5170  0.5841  0.5978  0.6508  ±0.21%      970
   · Old     514.10  1.7354  4.2685  1.9451  1.9701  2.2839  2.3610  4.2685  ±1.04%      258

 BENCH  Summary

   @tanstack/store  New - tests/foo.bench.ts > Derived
    3.77x faster than Old

For completeness, because this PR makes creating a Derived store more expensive than before, we can also do a benchmark of that to assess the damage (tl;dr 1.05x slower). The measured slowdown here seems consistent, but the effect size is so small it could virtually be ignored.

 ✓  @tanstack/store  tests/foo.bench.ts > Derived: setup 1294ms
     name      hz     min      max    mean     p75      p99     p995     p999      rme  samples
   · New   117.94  3.3893  94.4454  8.4791  9.2112  94.4454  94.4454  94.4454  ±43.33%       64
   · Old   123.41  2.7881   111.03  8.1031  3.3671   111.03   111.03   111.03  ±56.50%       62

 BENCH  Summary

   @tanstack/store  Old - tests/foo.bench.ts > Derived: setup
    1.05x faster than New

Copy link

nx-cloud bot commented Sep 20, 2025

View your CI Pipeline Execution ↗ for commit ec20d56

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 47s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 14s View ↗

☁️ Nx Cloud last updated this comment at 2025-09-24 16:50:13 UTC

Copy link

pkg-pr-new bot commented Sep 20, 2025

@tanstack/angular-store

npm i https://pkg.pr.new/@tanstack/angular-store@236

@tanstack/react-store

npm i https://pkg.pr.new/@tanstack/react-store@236

@tanstack/solid-store

npm i https://pkg.pr.new/@tanstack/solid-store@236

@tanstack/store

npm i https://pkg.pr.new/@tanstack/store@236

@tanstack/svelte-store

npm i https://pkg.pr.new/@tanstack/svelte-store@236

@tanstack/vue-store

npm i https://pkg.pr.new/@tanstack/vue-store@236

commit: ec20d56

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

Successfully merging this pull request may close these issues.

1 participant