Skip to main content

Performance

The examples below use sum, doubled, and chosen to show how pico-js stores dependencies and avoids repeated work. Unless an example gives a new starting state, it continues from the preceding one. The design document describes the update algorithm.

const a = createSourceNode(1);
const b = createSourceNode(2);
const useA = createSourceNode(true);

const sum = createDerivedNode(
(callingNode) => readSource(callingNode, a) + readSource(callingNode, b),
);
const doubled = createDerivedNode(
(callingNode) => readDerived(callingNode, sum) * 2,
);
const chosen = createDerivedNode((callingNode) =>
readSource(callingNode, useA)
? readSource(callingNode, a)
: readSource(callingNode, b),
);

const doubledRetain = retainDerivedNode(doubled);
retainDerivedNode(chosen);
// sum is 3, doubled is 6, and chosen is 1

Benchmark results​

On the Isograph application model, Pico was significantly faster than Alien Signals on 11 of 21 completed workloads and significantly slower on one. Against Preact Signals Core it was significantly faster on 12, with the other nine differences inconclusive. The table shows median event times for the application cases and elapsed time for the sparse-read graph benchmark.

WorkloadPicoAlien SignalsPreact Signals Core
Navigate to a screen with 30% unchanged records15.53 ms17.15 ms19.51 ms
Update a rarely changing field with 1,000 readers and independent rerenders2.33 µs21.89 µs18.26 µs
Read 10 leaves out of 10,000 after a write775.1 µs813.4 µs69.67 µs

These runs used the Pico engine at 26644a430, Alien Signals 3.2.1, Preact Signals Core 1.14.5, and Node 24.12.0. The full comparison includes per-scenario descriptions, p95 latency, work counts, and paired confidence intervals.

Edge lists​

Each dependency edge belongs to two lists. The dependent stores its source edges and derived edges in separate singly linked lists, in read order. Recomputing walks each list from the front and removes any unused suffix. The dependency stores a doubly linked list of its dependents, so pico-js can unlink any edge in constant time. The edge itself contains both sets of links. For example, the edge from sum to a belongs to both the source list of sum and the dependents list of a. Marking walks dependents. Verification walks derived dependencies.

Positional edge reuse​

A recompute reuses the edges of the previous run by position. The n-th source read takes the n-th source edge, the n-th derived read takes the n-th derived edge, and an edge moves only when its read is of a different node. Once the edges of the previous run are used up, a read adds an edge, unless it reads the same node as the previous read of its kind. Edges beyond the last read are removed. When sum recomputes and reads a and then b again, both edges stay and no edge is allocated. After writeSource(useA, false) and a read of chosen, its first source edge stays and its second moves from the dependents of a to the dependents of b.

A DIRTY node that is still needed keeps its edges until it recomputes, so its recompute reuses them instead of unlinking and relinking them. An update disposes an unneeded DIRTY node (see disposing nodes).

The source's record​

After writeSource(a, 10) walks its dependents, a records that every node that read it is already DIRTY. A computation that reads a clears the record. Until then, writeSource(a, 11) only stores the value and walks nothing, which saves the walk over the dependents of a source that is written repeatedly without being read in between.

Passing callingNode​

A computation passes its callingNode to readSource and readDerived. Nested computations can therefore record dependencies without saving and restoring a global current node. The public types prevent a caller from passing a source or a derived-node handle in place of a CallingNode.

One experimental engine build kept the current computation in a global. On a write followed by a read of a retained three-node chain, its timing was within noise of the explicit callingNode API. On a write that made one node reread 1,000 derived nodes, the global version took 4.7% longer. It took 1.4% to 8.2% longer in three of 22 application scenarios. The other differences were within noise.

A separate experimental wrapper also uses a closure for each read and dispatches on the node kind. In the original wrapper benchmark, the three-node and 1,000-dependency cases took 1.34 and 1.91 times as long as direct calls, respectively. Across 59 cases, batch(fn) took 3% to 27% longer than explicit batch calls in eight, mostly repeated write batches and effect churn. It was faster in three. The remaining differences were within noise.

Explicit stacks​

Marking, disposal, and the descent of verification through lazy MAYBE_DIRTY nodes walk the graph with explicit stacks, so a long chain of such nodes does not overflow the call stack. A chain of nodes that compute for the first time, or that are all DIRTY, recurses through the computations, and uses the call stack. Verification through MAYBE_DIRTY nodes with callbacks uses the call stack too, because verifyDerivedDependencies pulls a dependency with a callback, and that pull verifies the dependency in a call of its own. The contracts give the chain lengths at which each of these overflowed. A stack is allocated only when it is needed. Marking allocates one when a walk through lazy nodes branches, and allocates beyondCallbackStack when a queued callback node has reader edges to mark before its known-marked boundary. Verification allocates one when it descends into a node with several dependents. Disposal allocates one when a disposed node leaves a dependency that is not CLEAN unneeded.

Allocation​

An update that puts no node with a callback on the flush queue allocates no flush queue, and a batch commit allocates its flush queue on the first callback node it marks. An empty batch or a batch that writes one source needs no map. A batch that writes several sources stores the additional pending values in a map. The list of unneeded nodes is one module-level array that every update reuses. pico-js writes its entries by index and keeps a count of the entries in use. Clearing the list sets each entry in use to undefined and the count to 0, as the list of changed nodes does, so the array is never truncated, and a later update writes into the slots it already has.

Object shape and equality​

Pico uses separate source and derived edge helpers so each access to firstDependent sees only one kind of node. V8 can then specialize that access when its receivers share a hidden class. The representation of .value matters even when the helper only reads links. Derived nodes start with value: undefined. On Node 24.12, derived nodes that later held an integer, a double, a string, or an object all shared a hidden class. Sources start with the supplied value. In the same test, sources created with integers before the first double had a different hidden class from sources created afterward. V8 describes how field representations affect hidden classes.

New edges prepend at firstDependent, so neither node needs a tail pointer: a source node has 3 fields, and a derived node keeps its 12 fields with firstKnownMarkedDependentEdge. Equality is === only, with no comparison function to call.

State constants​

The runtime imports named states and bit masks from src/state.ts. Both package builds inline their numeric values, so a check such as node.status === CLEAN compiles to node.status === 3.

Runtime checks​

Starting a batch checks whether one is already open. Callers enforce the restrictions on reads, writes, and computations described in the contracts.