Memos — SolidJS

const doubled = createMemo(() => count() * 2) makes a cached derived value: computed once when created, computed again only when a source signal changes, then…

The search box at Nadia's Bookshop shows its result in two places: the matching book titles, and the line Found: 1. If the filter is written as a plain function, every keystroke filters the book list twice, once for each place that reads it. With three books you won't notice, but the work is still doubled.

A memo fixes this. It's a cached derived value: computed from other signals, kept, and only computed again when a source signal changes. If you come from React, it's like useMemo, minus the dependency list. createMemo(() => count() * 2) returns a getter, so it's read like a signal: doubled(). Think of Nadia looking for books with a certain word in the title, then writing the result on a small board by the till. Anyone who asks just reads that board. Nadia only walks the shelves again when the search word changes. A plain function is the opposite: every person who asks makes Nadia walk the shelves from the start.

createMemo: compute once, read many times

import { createSignal, createMemo } from "solid-js"

function App() {
  const [count, setCount] = createSignal(3)
  const doubled = createMemo(() => count() * 2)

  return (
    <div>
      <p>{count()} x 2 = {doubled()}</p>
    </div>
  )
}

The result is <p>3 x 2 = 6</p>.

That's the first exercise: count starting at 3, doubled as a memo, and a paragraph that puts both together.

A plain function vs createMemo

Nadia's Bookshop's search box, with two derived values: the search result and the cart total.

import { createSignal, createMemo } from "solid-js"

const books = [
  { title: "The Rainbow Troops", price: 18 },
  { title: "This Earth of Mankind", price: 24 },
  { title: "Beauty Is a Wound", price: 19 },
]

function App() {
  const [search, setSearch] = createSignal("")
  const [count, setCount] = createSignal(0)

  const results = createMemo(() => {
    console.log("filter runs")
    return books.filter(b => b.title.toLowerCase().includes(search().toLowerCase()))
  })
  const total = createMemo(() => {
    console.log("total computed")
    return count() * 18
  })

  return (
    <div>
      <input value={search()} onInput={e => setSearch(e.target.value)} placeholder="Search title" />
      <p>Results: {results().map(b => b.title).join(", ")}</p>
      <p>Found: {results().length}</p>
      <button onClick={() => setCount(c => c + 1)}>+1 Book</button>
      <p>Total: ${total()}</p>
      <p>Free shipping: {total() >= 30 ? "yes" : "not yet"}</p>
    </div>
  )
}

results() is read in two paragraphs, and so is total(). Here's the console, compared with the plain function version (const results = () => { ... } and const total = () => { ... }), tested in Solid 1.9:

Event With createMemo With a plain function
Page opens filter runs 1×, total computed 1× filter runs 2×, total computed 2×
Type m filter runs 1× filter runs 2×
Click +1 Book total computed 1× total computed 2×

A plain function runs once per place that reads it, so one letter prints filter runs twice. A memo runs once per change, however many places read it. The page looks the same either way: Results: This Earth of Mankind, Found: 1. The only difference is how often the work is repeated.

Notice too: typing doesn't run total, and a click doesn't run the filter. Each one only follows the signals it reads.

Signal, memo, or effect?

Use For Example
createSignal a value you change yourself through a setter the search box content, the cart count
createMemo a value computed from other signals, read-only the filter result, the total price
createEffect work outside the page saving to localStorage, document.title

A plain function such as () => price() * 0.9 is still fine for a cheap calculation read in one place. Once the calculation is heavy or read in many places, make it a memo.

The second exercise: odd or even

import { createSignal, createMemo } from "solid-js"

function App() {
  const [count, setCount] = createSignal(5)
  const isEven = createMemo(() => count() % 2 === 0)

  return (
    <div>
      <p>{count()} is {isEven() ? "Even" : "Odd"}</p>
      <button onClick={() => setCount(c => c + 1)}>+1</button>
    </div>
  )
}

💡 A memo only tells others when its value changes

A memo keeps its last result and compares it with the new one. If they're equal, nobody is told. Take const inStock = createMemo(() => stock() > 0): stock drops from 4 to 3, the memo computes again, the result is still true, so the text reading inStock() isn't touched. Only when stock reaches 0 does the result change to false, and the text follows.

⚠️ When the memo doesn't show or doesn't follow

TL;DR: const doubled = createMemo(() => count() * 2) makes a cached derived value: computed once when created, computed again only when a source signal changes, then read as {doubled()} any number of times without computing again. A plain function runs once per place that reads it: a filter result read in two paragraphs is filtered twice per keystroke, with a memo once. A memo is read-only and only tells its readers when its result changes. A signal for a value you change, a memo for a value that is computed, an effect for work outside the page.

⚡ createMemo: compute once, read many times

Write:

const [count, setCount] = createSignal(3)
const doubled = createMemo(() => count() * 2)

<p>{count()} x 2 = {doubled()}</p>

Meaning: A memo is a cached derived value. Solid computes it once, keeps the result (6), and only computes again when count changes. doubled is a getter, read with parentheses like a signal, and it has no setter.

⚡ A plain function vs a memo

Write:

const results = createMemo(() => {
  console.log("filter runs")
  return books.filter(b => b.title.toLowerCase().includes(search().toLowerCase()))
})
Guess first: results() is read in two paragraphs. If results were a plain function, how many times would filter runs print for each letter typed?

Twice, once for each place that reads it. With createMemo, only once, however many places read it.

Meaning: The page looks the same either way. The only difference is how often the work is repeated. A plain function is still fine for a cheap calculation read in one place.

⚡ Signal, memo, or effect?

Meaning: A signal for a value you change yourself through a setter. A memo for a value computed from other signals and only read. An effect for work outside the page, like localStorage or document.title.

What you will learn