Skip to content

perf(weakMapMemoize): return on a cache hit instead of rewriting the node - #771

Open
veksa wants to merge 1 commit into
reduxjs:masterfrom
veksa:perf/weak-map-memoize-hit-path
Open

perf(weakMapMemoize): return on a cache hit instead of rewriting the node#771
veksa wants to merge 1 commit into
reduxjs:masterfrom
veksa:perf/weak-map-memoize-hit-path

Conversation

@veksa

@veksa veksa commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

On a cache hit memoized reads the result out of the node and then falls through to the same two writes a miss does:

if (cacheNode.s === TERMINATED) {
  result = cacheNode.v
} else {
  // ...compute
}
terminatedNode.s = TERMINATED
terminatedNode.v = result

Both are no-ops on that path — s is already TERMINATED and v already holds exactly the result we just read out of it. The v write isn't free, though: it stores a pointer into a heap object, so it costs a GC write barrier on a call that had nothing to record. This returns early instead.

The diff looks bigger than it is — the else branch loses a level of indentation.

Measured with the harness from #770 (paired against master in one process, min of 15 interleaved rounds, ns/call):

case before after
1 input, (state) 12.0 11.1 1.08x
nested (2 output selectors) 12.3 11.5 1.06x
3 inputs, (state) 11.8 11.3 1.05x
1 input, (state, props) 177.1 176.2 noise
same, argsMemoize: lruMemoize 78.2 75.2 noise

Only the hit path moves, which is what you'd expect: the parametric cases never hit the argument cache (new state every tick, different props every call), so there's no redundant write to skip there. Recomputation counts are identical everywhere, so it's the same work either way.

@netlify

netlify Bot commented Aug 7, 2026

Copy link
Copy Markdown

Deploy Preview for reselect-docs canceled.

Name Link
🔨 Latest commit 067f4c8
🔍 Latest deploy log https://app.netlify.com/projects/reselect-docs/deploys/6a759283a27ad0000821c546

@codesandbox-ci

codesandbox-ci Bot commented Aug 7, 2026

Copy link
Copy Markdown

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant