Title: Memoisation in JavaScript – Unbounded Cache Crashes
Open Graph Title: Memoisation in JavaScript – Unbounded Cache Crashes
Description: After 200,000 entries, an unbounded memoisation cache consumed 300MB and crashed the pod. Bound your cache with LRU or TTL to prevent OOM in production.
Open Graph Description: After 200,000 entries, an unbounded memoisation cache consumed 300MB and crashed the pod. Bound your cache with LRU or TTL to prevent OOM in production.
Opengraph URL: https://thecodeforge.io/javascript/memoisation-javascript/
Domain: thecodeforge.io
{
"@context": "https://schema.org",
"@type": "TechArticle",
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://thecodeforge.io/javascript/memoisation-javascript/"
},
"headline": "Memoisation in JavaScript – Unbounded Cache Crashes",
"description": "After 200,000 entries, an unbounded memoisation cache consumed 300MB and crashed the pod. Bound your cache with LRU or TTL to prevent OOM in production.",
"keywords": "Memoisation in JavaScript – Unbounded Cache Crashes, JavaScript, Advanced, Advanced JS, After 200,000 entries, an unbounded memoisation cache consumed 300MB and crashed the pod. Bound your cache with LRU or TTL to prevent OOM in production.",
"proficiencyLevel": "Expert",
"author": {
"@type": "Person",
"@id": "https://thecodeforge.io/about/#naren",
"name": "Naren",
"url": "https://thecodeforge.io/about/"
},
"publisher": {
"@type": "Organization",
"@id": "https://thecodeforge.io/#org",
"name": "TheCodeForge",
"url": "https://thecodeforge.io",
"logo": {"@type": "ImageObject", "url": "https://thecodeforge.io/assets/og-image.png"}
},
"datePublished": "2026-03-06T00:00:00+00:00",
"dateModified": "2026-08-30T00:00:00+00:00",
"url": "https://thecodeforge.io/javascript/memoisation-javascript/",
"inLanguage": "en",
"educationalLevel": "Advanced",
"image": "https://thecodeforge.io/assets/og-image.png",
"isPartOf": {"@id": "https://thecodeforge.io/#website"}
}
{
"@context": "https://schema.org",
"@type": "BreadcrumbList",
"itemListElement": [
{"@type": "ListItem", "position": 1, "name": "Home", "item": "https://thecodeforge.io/"},
{"@type": "ListItem", "position": 2, "name": "JavaScript", "item": "https://thecodeforge.io/javascript/"},
{"@type": "ListItem", "position": 3, "name": "Memoisation in JavaScript – Unbounded Cache Crashes", "item": "https://thecodeforge.io/javascript/memoisation-javascript/"}
]
}
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "What is the difference between memoisation and caching in JavaScript?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Caching is the general concept of storing computed results for reuse. Memoisation is a specific form of caching that is tied directly to a function — it caches the return value of a pure function keyed by its input arguments, and the cache is scoped to that function's closure. General caching might involve HTTP responses, database queries, or arbitrary data stores with manual invalidation logic."
}
},
{
"@type": "Question",
"name": "Is JavaScript's useMemo the same as writing a memoisation function?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Not quite. React's useMemo is component-scoped and has a cache size of exactly one: it only remembers the result from the most recent render, and only reuses it if the dependency array hasn't changed. A general memoisation utility grows its cache across every unique set of arguments it has ever seen. useMemo is optimised for referential stability between renders, not for avoiding repeated expensive computations across different inputs."
}
},
{
"@type": "Question",
"name": "Does memoisation always make a function faster?",
"acceptedAnswer": {
"@type": "Answer",
"text": "No. Memoisation adds overhead: it must compute the cache key, perform a Map lookup, and potentially serialise arguments on every single call — even cache misses. For functions that are already very fast (simple arithmetic, single property access), this overhead can actually make them slower than the un-memoised version. Memoisation pays off when the function being wrapped is significantly more expensive than the cache lookup, and when the same arguments are genuinely repeated across calls."
}
},
{
"@type": "Question",
"name": "How do I clear a memoised function's cache?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Expose the cache or provide a `clear()` method. For the implementation shown in this article, you can modify the wrapper to return an object with both the memoised function and a clear method: `return { fn: memoisedFunction, clear: () => cache.clear() }`. Alternatively, reassign the function variable to a fresh memoised version."
}
},
{
"@type": "Question",
"name": "Can I memoise async functions?",
"acceptedAnswer": {
"@type": "Answer",
"text": "Yes, but be careful. If the async function is called multiple times with the same arguments before the first call resolves, you'll launch multiple concurrent computations unless you also cache the promise itself. A common pattern is to cache the returned promise, so subsequent calls await the same promise. Also remember that an async function that calls external APIs is impure — the API response may change between calls, so add a TTL to the cache."
}
}
]
}
| og:type | website |
| og:image | https://thecodeforge.io/assets/og-image.png |
| og:image:width | 1200 |
| og:image:height | 630 |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: max-image-preview:large