Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 39 ยท Signals and Reactivity

Derived Values and createMemo

Learn how derived values work in SolidJS: plain functions, createMemo caching, equality checks, memo chains and when a memo is not worth it.

Most state in an app is not stored, it is derived: a total from a list of prices, a filtered list from a search box, a "can submit" flag from three form fields. Solid gives you two ways to derive values, and the difference matters for both speed and correctness. This lesson covers both, and shows how to choose.

A derived value is just a function

The simplest derived value is a function that reads signals. Because the function calls a getter, whoever calls the function gets tracked.

JavaScript
const [price, setPrice] = createSignal(20);
const [qty, setQty] = createSignal(3);

const total = () => price() * qty();   // derived, no library call needed

createEffect(() => console.log("total:", total()));
setQty(4);   // logs "total: 80"

total is not a signal and holds no value. Each time you call total(), it recomputes. That is perfectly fine when the calculation is cheap, and it keeps working with no extra concepts.

Derived values as plain functions

Why you might want a memo

A plain function has one weakness: it recalculates for every reader. If five places in your UI call total(), the multiplication runs five times per change. For a multiplication that is nothing. For sorting ten thousand rows, it is a problem.

createMemo wraps a computation so that it runs once per change and caches the result. Everything that reads the memo shares the cached value.

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

const sorted = createMemo(() => [...items()].sort(byName));
// sorted() returns the cached array until items() changes

A memo is both a reader and a source. It subscribes to the signals it reads, and other effects or memos can subscribe to it. It only notifies its own subscribers when its result actually changes.

Counting how often a computation runs

After the first click you should see the plain function has run far more often than the memo. Three reads of the memo cost one computation; three calls of the plain function cost three.

Memos stop the ripple

A memo also acts as a gate. Suppose a memo returns a boolean:

JavaScript
const isAdult = createMemo(() => age() >= 18);

If age goes from 30 to 31, the memo re-runs but still returns true. Solid compares the new result with the old one using === and, because they are equal, does not notify anything that depends on isAdult. A plain function would have no such shield: its readers would re-run on every age change.

You can change the comparison with the equals option, which is useful for objects:

JavaScript
const bounds = createMemo(
  () => ({ w: width(), h: height() }),
  undefined,
  { equals: (a, b) => a.w === b.w && a.h === b.h }
);

Pass equals: false to notify subscribers on every run, even when the value looks the same.

A memo that hides irrelevant changes

Click the button several times and watch the console. The "age effect" logs on every click; the "isAdult effect" logs only when the answer flips from false to true.

Chains of memos

Memos can read other memos. This builds a small dependency graph in which each step runs only when its inputs really changed.

JavaScript
const visible   = createMemo(() => todos().filter(matchesFilter));
const remaining = createMemo(() => visible().filter(t => !t.done).length);

Solid keeps the graph glitch-free: if one signal feeds two memos that feed a third, the third runs once with consistent values, never with one new and one stale input.

When not to use createMemo

A memo is not free: it allocates a node in the graph and tracks subscriptions. Use it when:

  • the computation is expensive (sorting, filtering big arrays, parsing),
  • the result is read in several places,
  • you want to cut off downstream updates when the result is unchanged.

Skip it for () => a() + b(), string concatenation, or property access. A plain function is clearer and just as fast. Also remember that memos are for pure calculations: do not write signals or touch the DOM inside one. Side effects belong in createEffect.

Common mistakes

  • Writing signals inside a memo. It makes the graph hard to reason about. Return a value instead.
  • Forgetting to call it. A memo is read as value(), like a signal.
  • Memoizing everything. Premature memos add noise. Measure, then add.
  • Reading non-reactive data. A memo over a plain variable never updates, just like an effect.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does createMemo do that a plain derived function does not?

    A memo stores its last result and shares it, so it recomputes only when its sources change.
  2. 2. A memo returns true, then re-runs and returns true again. What happens to its subscribers?

    Solid compares the new result to the old with === (or your equals function) and only notifies on a change.
  3. 3. Which case is the best fit for createMemo?

    Expensive, shared computations benefit from caching; trivial ones do not.
  4. 4. How do you force a memo to notify even when the new value equals the old?

    The equals option controls the comparison; false means every run notifies.

Summary

  • Derived values can be plain functions: simple, always fresh, recomputed per reader.
  • createMemo caches the result and shares it among readers.
  • A memo only notifies subscribers when its result changes (=== by default, customizable with equals).
  • Memos chain into a glitch-free graph; keep them pure.
  • Use them for expensive or widely shared calculations, not for trivial ones.

Next: Batching, untrack and on