Skip to content

fix(cache): hash memoize and cache keys without collisions - #81

Merged
tabkram merged 1 commit into
mainfrom
fix/cache-key-collisions
Nov 15, 2026
Merged

tabkram merged 1 commit into
mainfrom
fix/cache-key-collisions

Conversation

@tabkram

@tabkram tabkram commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Closes #79

Problem

The default key of executeMemoize and executeCache was sha256(JSON.stringify(inputs)).
JSON merges Map, Set and RegExp into {} and undefined, NaN and null into null, so distinct
arguments shared one entry and returned another call's result. BigInt and circular
references threw before the wrapped function ran.

Normalize inputs before hashing so these values stay distinguishable: Map, Set, RegExp,
Date, URL, Error, typed arrays, class instances, undefined, NaN, Infinity, -0, BigInt,
symbols, functions and circular references. Plain JSON values keep their previous hash.

The default key of executeMemoize and executeCache was sha256(JSON.stringify(inputs)).
JSON merges Map, Set and RegExp into {} and undefined, NaN and null into null, so distinct
arguments shared one entry and returned another call's result. BigInt and circular
references threw before the wrapped function ran.

Normalize inputs before hashing so these values stay distinguishable: Map, Set, RegExp,
Date, URL, Error, typed arrays, class instances, undefined, NaN, Infinity, -0, BigInt,
symbols, functions and circular references. Plain JSON values keep their previous hash.
@github-actions

Copy link
Copy Markdown
Contributor

Coverage report

St.❔
Category Percentage Covered / Total
🟢 Statements
96.76% (+0.4% 🔼)
567/586
🟢 Branches
90.95% (+1.41% 🔼)
432/475
🟢 Functions
95.8% (+0.28% 🔼)
137/143
🟢 Lines
97.16% (+0.35% 🔼)
547/563
Show files with reduced coverage 🔻
St.❔
File Statements Branches Functions Lines
🟢
... / crypto.ts
100%
98.44% (-1.56% 🔻)
100% 100%

Test suite run success

179 tests passing in 15 suites.

Report generated by 🧪jest coverage report action from b02c360

@tabkram
tabkram merged commit 5253ae0 into main Nov 15, 2026
9 checks passed
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.

[BUG] cache/memoize: input hash collides on Map/Set/undefined/NaN and throws on BigInt

1 participant