Manual memoization

수동 메모이제이션

Reuse a value or function reference while dependencies stay equal to avoid unnecessary work.

···
html
<div class="scene"><div class="title">PARENT UPDATE <span id="turn">1</span></div><div class="row"><div class="label">raw compute</div><div class="cells" id="raw"></div></div><div class="row"><div class="label">memoized</div><div class="cells" id="memo"></div></div><div class="foot" id="foot">same dependencies → cached result</div></div>
css
.scene{width:min(94vw,800px);height:min(86vh,320px);position:relative;overflow:hidden;display:flex;flex-direction:column;justify-content:space-between;border:1px solid var(--line);border-radius:14px;background:var(--surface);padding:clamp(9px,2.4vmin,20px);font:500 clamp(15px,4vmin,18px)/1.3 var(--font-sans,-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif)}.scene b,.scene strong{font-weight:700}.label{color:var(--muted)}.code{font-family:ui-monospace,SFMono-Regular,Consolas,monospace}.pill{border:1px solid var(--line);border-radius:8px;padding:.4em .65em;background:var(--bg)}.active{border-color:var(--accent)!important;color:var(--accent)!important;background:color-mix(in srgb,var(--accent) 10%,var(--surface))!important}.title{font-weight:700;color:var(--accent)}.row{display:flex;align-items:center;gap:8px;margin:clamp(12px,5vmin,30px) 0}.row .label{width:26%;font-size:.85em}.cells{display:flex;gap:4px;flex:1}.cells i{height:clamp(22px,8vmin,52px);flex:1;border-radius:5px;background:var(--accent-3);opacity:.35}.cells i.on{opacity:1}.row:last-of-type .cells i{background:var(--accent)}.foot{font-size:.85em;color:var(--muted)}
js
let n=1;for(const id of ["raw","memo"]){document.getElementById(id).innerHTML="<i></i><i></i><i></i><i></i><i></i>"}function tick(){document.getElementById("turn").textContent=n;[...document.querySelectorAll("#raw i")].forEach((x,i)=>x.classList.toggle("on",i<n%6));[...document.querySelectorAll("#memo i")].forEach((x,i)=>x.classList.toggle("on",i===0));n=n%5+1}tick();setInterval(tick,850)

useMemo reuses a calculated value, and useCallback reuses a function reference according to dependencies. Together with React.memo, this may let a child skip rendering.

The demo compares repeated computation with reuse during unrelated parent updates. Memoization has a cost, so measure before adding it everywhere.

When to use

Use it after profiling reveals expensive calculation or unstable references as a bottleneck.

Open as page ↗