Pulse: Building a Small Reactive Runtime
A compact look at signals, lazy computed values, tracked effects, batching and instrumentation in a framework-independent JavaScript reactive runtime.
Frameworks makes reactivity feel like an API. Pulse has a small surface area with four functions, and the application is left to compose components.
signal is a mutable value. Reading it in a computation subscribes to it. Writing to it marks its dependents as stale. computed is lazy: it invalidates the graph and runs its function when it is read. effect runs synchronously and tracks what was read. It runs again when its dependencies change, and onCleanup and the disposer help avoid leaks. batch delays the flush until the outermost batch ends, so we don’t publish the intermediate state between two writes.
import { batch, computed, effect, signal } from '@weluvabid/pulse';
const count = signal(0);
const doubled = computed(() => count() * 2);
const dispose = effect(() => console.log(doubled()));
batch(() => {
count.set(1);
count.set(2);
});
dispose();
Pulse uses a push-only system, so it may do computations that nobody needs. A pull-only system would have to check whether a value is fresh each time it is read. This is the balance: writes mark dependents as stale, and computed values are refreshed when they are read. Effects run synchronously after the update boundary. If batches are nested, the flush waits until the outermost batch is complete.
On each effect run, Pulse tracks what was actually read and replaces the previous dependencies. If a conditional branch changes, subscriptions from the previous branch are removed. Writes to those signals will no longer schedule the effect.
Before an effect is re-run, its child effects are disposed of and cleanups run in reverse registration order. The disposer is idempotent and removes the effect from its dependencies. The scheduler de-duplicates queued effects in a Set. There is a limit on synchronous re-entry so a cycle cannot block the thread indefinitely. If something fails, the errors are collected and the rest of the queue still gets a chance to run.
onReactiveEvent exposes event types and runtime IDs, but never application values. Errors thrown by listeners are caught so instrumentation cannot interrupt the scheduler.
Pulse provides reactive state and effect scheduling. Rendering and component lifecycles remain with the application. Effects always run synchronously, so a slow effect will block the JavaScript agent. Batching only affects when queued effects are flushed.
The tests I care about are the annoying graphs: conditional dependencies, nested batches, cleanups in the correct order, disposing during an update and a listener that throws. Benchmarks should separate runtime overhead from the effect itself. Otherwise, you’re just benchmarking how slow your console.log is.
const usePrimary = signal(true);
const primary = signal('A');
const secondary = signal('B');
const dispose = effect(() => {
console.log(usePrimary() ? primary() : secondary());
});
usePrimary.set(false); // next run drops primary and tracks secondary
dispose();
The dependency set is rebuilt on each run. Once the flag has changed, writing to primary should no longer schedule this effect.
Related writing
- The Cost of Doing Work Too Early
Lazy preparation, cached work and deferred computation reduce repeated cost when the application can identify the moment a result is actually needed.
- Building a Reactive Runtime: Dependency Graphs and Scheduling
A closer look at dependency reconciliation, lazy computed values, effect ownership and synchronous scheduling in Pulse.
- Making Small Libraries Production-Ready
The contracts, tests, package boundaries and release discipline that turn a focused algorithm or runtime into a reusable library.