Skip to content
@pyreon/state-tree
fundamentals · structured state

The MobX-State-Tree model, with 9.5× faster actions.

Models with views and actions, JSON snapshots, patches you can record and replay, middleware, references by id. The ideas MobX-State-Tree made popular, built on signals instead of observables, and measured against MST 9 op for op.

bun add @pyreon/state-tree
Documentation
vs MobX-State-Tree 9
faster on all 5 published ops2.2× to 9.5×, bun, one run.
tracks
snapshots · patches · actions
schemas
Zod, Valibot, ArkType, sSchema mode validates every set, patch and update.
native
iOS and Android, experimentalmodel() compiles to a native singleton.
Every action emits a patch and a snapshot; undo applies one back A Counter model created with count 5. counter.increment() sets count to 6; onPatch receives { op: 'replace', path: '/count', value: 6 } and onSnapshot adds { count: 6 } to the history. Undo calls applySnapshot(counter, { count: 5 }): the count returns to 5 and a new patch with value 5 is emitted. @PYREON/STATE-TREE · PATCHES AND SNAPSHOTS const Counter = model({ state: { count: 0 } }) .actions((self) => ({ increment: … })) counter.count() 5 6 onPatch waiting for an action… { op: 'replace',path: '/count',value: 6 } { op: 'replace',path: '/count',value: 5 } send it, log it, applyPatch it onSnapshot · history { count: 4 } { count: 5 } { count: 6 } counter.increment() applySnapshot(counter, { count: 5 }) undo is a snapshot
  • 9.5× faster action calls than MobX-State-Tree 9.BENCHMARKS.md §10 state-tree row, overnight run 2026-11-05, bun/JavaScriptCore, one run
  • 8.3× faster observer notification.BENCHMARKS.md §10 state-tree row, one run
  • 7.5× faster applyPatch; applySnapshot with a schema 6.8×.BENCHMARKS.md §10 state-tree row, one run

synthetic · author-run · M3 Max · record of 2026-11-05

Every result, including where we lose →
why teams pick it

For state that has real structure.

  1. History you don't have to build

    onSnapshot hands you plain JSON after each burst of changes (coalesced per microtask), and applySnapshot puts it back. Undo, time travel and persistence are an array and two calls.

  2. Patches out, patches in

    Every write emits a JSON patch with a path like /profile/name, nested models included. Send them to a server, log them, or replay them with applyPatch.

  3. Middleware that can say no

    addMiddleware(node, (call, next) => …) sees every action with its name and arguments. Don't call next and the action never runs: read-only modes, permission checks, audit logs.

  4. Signals underneath

    Every state field is a Signal, every view is read like one. Components bind to todo.done() directly, with no observer() wrapper and no proxy.

the difference

Same model, minus the observable machinery.

MobX-State-Tree 9

types.* declarations, getters for views, mutation through self inside actions.

import { getSnapshot, onPatch, types } from 'mobx-state-tree'

const Todo = types
  .model('Todo', { id: types.identifier, title: types.string, done: false })
  .actions((self) => ({
    toggle() {
      self.done = !self.done
    },
  }))

const TodoList = types
  .model('TodoList', { todos: types.array(Todo) })
  .views((self) => ({
    get left() {
      return self.todos.filter((t) => !t.done).length
    },
  }))
  .actions((self) => ({
    add(title: string) {
      self.todos.push({ id: String(self.todos.length + 1), title })
    },
  }))

const list = TodoList.create({ todos: [] })
onPatch(list, (patch) => console.log(patch))
list.add('Write docs')
list.todos[0]!.toggle() // { op: 'replace', path: '/todos/0/done', value: true }
console.log(list.left, getSnapshot(list))
with @pyreon/state-tree

Plain initial values, views as functions, signals in the actions.

import { getSnapshot, model, onPatch } from '@pyreon/state-tree'

type Todo = { id: string; title: string; done: boolean }

const TodoList = model({ state: { todos: [] as Todo[] } })
  .views((self) => ({
    left: () => self.todos().filter((t) => !t.done).length,
  }))
  .actions((self) => ({
    add: (title: string) =>
      self.todos.update((xs) => [...xs, { id: String(xs.length + 1), title, done: false }]),
    toggle: (id: string) =>
      self.todos.update((xs) => xs.map((t) => (t.id === id ? { ...t, done: !t.done } : t))),
  }))

const list = TodoList.create()
onPatch(list, (patch) => console.log(patch))
list.add('Write docs')
list.toggle('1') // { op: 'replace', path: '/todos', value: [{ id: '1', …, done: true }] }
console.log(list.left(), getSnapshot(list))
coming from

Coming from MobX-State-Tree?

Same concepts, different plumbing.

MobX-State-Tree
import { getSnapshot, onPatch, types } from 'mobx-state-tree'

const Todo = types
  .model('Todo', { id: types.identifier, title: types.string, done: false })
  .actions((self) => ({
    toggle() {
      self.done = !self.done
    },
  }))

const TodoList = types
  .model('TodoList', { todos: types.array(Todo) })
  .views((self) => ({
    get left() {
      return self.todos.filter((t) => !t.done).length
    },
  }))
  .actions((self) => ({
    add(title: string) {
      self.todos.push({ id: String(self.todos.length + 1), title })
    },
  }))

const list = TodoList.create({ todos: [] })
onPatch(list, (patch) => console.log(patch))
list.add('Write docs')
list.todos[0]!.toggle() // { op: 'replace', path: '/todos/0/done', value: true }
console.log(list.left, getSnapshot(list))
Pyreon

Views become functions, self.done = … becomes self.done.set(…), types.* becomes initial values or a schema. Both runs print the same final snapshot. The patch differs: MST reports /todos/0/done, Pyreon replaces /todos.

import { getSnapshot, model, onPatch } from '@pyreon/state-tree'

type Todo = { id: string; title: string; done: boolean }

const TodoList = model({ state: { todos: [] as Todo[] } })
  .views((self) => ({
    left: () => self.todos().filter((t) => !t.done).length,
  }))
  .actions((self) => ({
    add: (title: string) =>
      self.todos.update((xs) => [...xs, { id: String(xs.length + 1), title, done: false }]),
    toggle: (id: string) =>
      self.todos.update((xs) => xs.map((t) => (t.id === id ? { ...t, done: !t.done } : t))),
  }))

const list = TodoList.create()
onPatch(list, (patch) => console.log(patch))
list.add('Write docs')
list.toggle('1') // { op: 'replace', path: '/todos', value: [{ id: '1', …, done: true }] }
console.log(list.left(), getSnapshot(list))
try it

Toggle todos, then undo.

Toggle and add todos. Each change prints its JSON patch, and undo restores the previous snapshot with applySnapshot.

Todos

1 left

onPatch
  1. Toggle or add a todo.
getSnapshot
{
  "todos": [
    {
      "id": 1,
      "title": "Write the spec",
      "done": true
    },
    {
      "id": 2,
      "title": "Ship the demo",
      "done": false
    }
  ]
}
benchmarks

Faster than MobX-State-Tree on all five published ops: 2.2× to 9.5×.

Models, snapshots and patches, op for op against the library that defined the idea. No op in this row was a tie or a loss.

vs MobX-State-Tree 9.0.0 · bun / JavaScriptCore, one process per opPyreonrival · lower is better
  • action
    win9.5×
  • observer
    win8.3×
  • applyPatch
    win7.5×
  • applySnapshot (schema)
    win6.8×
  • create (schema)
    win2.2×
Apple M3 Max · record of 2026-11-05 · overnight run, one run · author-run, syntheticFull method, every loss →
what's included

The model toolkit.

  • model({ state }) then .views(), .actions(), .volatile(), .lifecycle(); finish with .create() or a singleton .asHook(id).
  • Schema mode model({ schema }) with s, Zod, Valibot or ArkType; set, patch, deepPatch, update validate before writing.
  • Async actions an action can await; it is still one action to middleware.
  • Volatile state reactive, but left out of snapshots and patches.
  • Snapshots getSnapshot, applySnapshot (schema mode re-validates), onSnapshot.
  • Patches onPatch and applyPatch, single or batched.
  • Middleware addMiddleware can block or wrap; onAction only observes.
  • Tree navigation getParent, getRoot, getPath, hasParent, isRoot.
  • References identifier() and reference(Type) store an id and resolve to the live node; snapshots keep the id.
  • Lifecycle afterCreate, beforeDestroy, destroy, isAlive, clone, getType.
recipes

Things to build with it.

Undo, and a read-only switch

Snapshots go into an array; undo applies the previous one. One middleware blocks every action while readOnly is on.

import { addMiddleware, applySnapshot, getSnapshot, model, onSnapshot } from '@pyreon/state-tree'

const Editor = model({ state: { text: '', fontSize: 14, readOnly: false } })
  .actions((self) => ({
    type: (t: string) => self.text.update((s) => s + t),
    zoom: (by: number) => self.fontSize.update((n) => n + by),
  }))

const doc = Editor.create()
const past = [getSnapshot(doc)]

// one snapshot per burst of changes (onSnapshot is microtask-coalesced)
onSnapshot(doc, (snap) => past.push(snap))

// middleware sees every action and can block it
addMiddleware(doc, (call, next) => (doc.readOnly() ? undefined : next(call)))

doc.type('Hello')
await Promise.resolve()
doc.zoom(2)
await Promise.resolve()

const undo = () => {
  if (past.length < 2) return
  past.pop() // the current state
  // the restored state is pushed back by onSnapshot on the next microtask
  applySnapshot(doc, past.pop()!)
}
undo()
await Promise.resolve()
console.log(doc.text(), doc.fontSize()) // 'Hello' 14
api

Models, and the functions that watch them.

Every export has a reference page with its signature, an example and the mistakes people make with it.

models · 7
  • model
  • clone
  • destroy
  • isAlive
  • getType
  • resetHook
  • resetAllHooks
snapshots and patches · 5
  • getSnapshot
  • applySnapshot
  • onSnapshot
  • onPatch
  • applyPatch
middleware · 2
  • addMiddleware
  • onAction
tree and references · 8
  • getParent
  • getRoot
  • getPath
  • hasParent
  • isRoot
  • identifier
  • reference
  • resolveIdentifier
types · 5
  • ModelInstance
  • Snapshot
  • Patch
  • ActionCall
  • MiddlewareFn
good to know

Good to know before you start.

Patches are per field

A write replaces a field, so changing one item in an array emits one replace of the whole array at /todos. MobX-State-Tree emits /todos/0/done. Snapshots and undo are unaffected.

Keep collections as plain data

Hold arrays of plain objects and put the actions on the parent, as the docs' todo example does. A model instance stored inside an array field doesn't yet produce replayable patches; nested models as named fields do.

Schema mode is flat

Nested models compose in plain mode. A schema-mode model validates its own fields.

More structure than a store

For a few shared values @pyreon/store is less ceremony. Reach for state-tree when you want snapshots, patches or middleware.

questions

What people ask first.

How was it measured against MST?

One process per op and library, under bun, in one overnight run that we wrote and judged. Every op in the row is a win, but run it before you rely on it: packages/fundamentals/state-tree/bench/.

Do I need observer() around components?

No. Pyreon components run once and bind each signal read to the DOM node that shows it, so reading doc.text() in JSX is all it takes.

Can I send patches to a server?

Yes: onPatch gives { op, path, value } objects as plain JSON, and applyPatch applies them on the other side. Only replace operations exist.

Model it once. Undo comes with it.

bun add @pyreon/state-tree
Read the state-tree guide