Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 39 ยท Testing and Performance

Performance: Fine-Grained Updates in Practice

Make SolidJS apps fast in practice. Measure updates, avoid accidental reruns, use createMemo, batch, createSelector, keyed lists, untrack and smaller bundles.

Solid is fast by design: components run once and only the exact DOM nodes that depend on a changed signal update. For most apps the best performance advice is "write it the obvious way". But there are situations where work is repeated, lists are large, or the bundle is heavy, and then a few tools matter.

This lesson shows how to see what updates, then goes through the techniques in order of how often you will need them. Measure first: a fast tool you did not need is just more code.

Seeing what runs

Because there is no virtual DOM diff, performance questions become: which computations re-run when this signal changes, and how much work does each do? Put a counter in suspicious places:

Count what re-runs

At start, the log shows expensive() ran four times: three for the plain function (each reader runs it) and once for the memo. Each click repeats this. The result of a plain derived function is not cached, which is usually fine for cheap work and wasteful for expensive work.

1. createMemo for expensive derived values

A memo caches its result and is shared by all readers; it re-runs only when its dependencies change, and it only notifies readers when the value actually changes (compared with ===).

JavaScript
const filtered = createMemo(() => items().filter((i) => i.name.includes(query())));
// 3 components read filtered(): the filter runs once per change, not three times.

Use memos for derived values that are expensive (filtering and sorting thousands of rows, parsing) or read in many places. Do not wrap trivial expressions such as count() * 2; the memo bookkeeping costs more than it saves.

A useful side effect: a memo that returns a boolean or enum shields downstream code from noisy inputs. createMemo(() => width() > 600) only notifies when the answer flips, not on every pixel of resizing.

2. batch to group writes

Each signal write triggers its dependents immediately. Several writes in a row can therefore cause several updates of the same effect. batch defers updates until the end of the callback:

JavaScript
import { batch } from "solid-js";

batch(() => {
  setFirst("Ada");
  setLast("Lovelace");
  setAge(36);
});     // effects that read all three run once, after the batch

Writes made inside an effect and store path updates are batched automatically, but plain signal writes in event handlers, timers and after an await are not: each one updates its readers at once. Reach for batch whenever several writes should look like a single change to your effects and DOM bindings.

3. Lists: For, Index and keyed identity

Use <For> for lists of objects (keyed by reference): moving or reordering an item moves its DOM node, and changing an item's property updates only that property's bindings. Use <Index> when the list is primitives (strings, numbers) or when position matters more than identity: it keeps a node per index and updates the content through a signal.

JSX
<For each={users()}>{(u) => <Row user={u} />}</For>        // objects: For
<Index each={words()}>{(w, i) => <li>{i}: {w()}</li>}</Index> // primitives: Index, w is a signal

With For, swapping two objects in the array moves two DOM nodes; no component is re-created. If you replace the array with new objects that look equal (for example after each fetch), For sees new references and re-creates every row. Keep identity stable: for a fetched list in a store, update with reconcile (produce, reconcile and unwrap), which diffs by key and mutates in place.

4. createSelector for "which one is selected"

Suppose 1,000 rows each check selectedId() === row.id. When the selection changes, all 1,000 bindings re-run, to learn that 998 of them still say false. createSelector turns that into two updates: the row that lost selection and the row that gained it.

JSX
const [selected, setSelected] = createSignal(1);
const isSelected = createSelector(selected);

<For each={rows()}>
  {(row) => (
    <li classList={{ active: isSelected(row.id) }} onClick={() => setSelected(row.id)}>
      {row.label}
    </li>
  )}
</For>
createSelector: two updates instead of many

Every click shows 200 naive re-runs against about 2 with the selector. On a list of 5,000 rows that is the difference you can feel.

5. untrack and on: control what is tracked

Sometimes an effect reads a signal but should not re-run when it changes. untrack reads without subscribing, and on states the dependencies explicitly:

JavaScript
createEffect(() => {
  console.log("user changed to", user(), "at", untrack(clock));  // only `user` triggers
});

createEffect(on(user, (u, prev) => save(u, prev), { defer: true }));  // skips the first run

Explicit dependencies are also a good way to make a complicated effect readable.

6. Do less on load: bundle and rendering

  • Split code with lazy (lesson) at route and heavy-widget level.
  • Prefer built-in control flow (Show, For, Switch) to ternaries that create large trees; they create and dispose nodes in the right moments.
  • Avoid creating big objects inside reactive expressions. style={{ ... }} creates a new object per run; for static styles use a class, for dynamic values use style={{ width: w() + "px" }}, which Solid diffs by property.
  • Virtualize very long lists (thousands of rows visible at once): render only the window the user can see. Libraries such as @tanstack/solid-virtual do this.
  • Check the bundle: Solid itself is small, so the weight is usually your dependencies. Analyze the build output and lazy-load heavy ones such as charting and editors.
  • Server-render and stream with SolidStart so the first content appears before the JavaScript runs (overview).

Measuring

Use the browser Performance panel to record an interaction and look for long tasks, and console.time or counters like the one above for specific computations. The debugging lesson covers Solid-specific tools. When something is slow, find what re-runs and how often before changing code.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When does createMemo re-run its function?

    A memo caches its value, re-computes when a tracked signal changes and only notifies dependents on a different value.
  2. 2. Which list component fits an array of primitive strings where position matters?

    Index keeps one node per position and passes a signal for the item, which suits primitives.
  3. 3. What does createSelector optimize?

    Instead of every row re-running its comparison, only the two affected rows update.
  4. 4. When do several plain signal writes need batch to count as one update?

    Each plain write updates its readers immediately unless it is inside batch (effects and store setters batch on their own).

Summary

  • Find what re-runs before optimizing; plain derived functions run for every reader.
  • createMemo caches expensive derived values and shields readers from irrelevant changes.
  • batch groups plain signal writes into one update; untrack and on control tracking.
  • For for objects, Index for primitives; keep object identity stable with reconcile.
  • createSelector makes selection O(1) updates; lazy-load, virtualize and server-render for load speed.

Next: Debugging and Developer Tools