Memoize II - Problem

You're tasked with creating an advanced memoization function that can optimize any given function by caching its results. Unlike basic memoization, this version must handle complex input types including objects, arrays, and nested structures.

Given a function fn, return a memoized version that:

  • Never calls the original function twice with identical inputs
  • Returns cached results for previously seen argument combinations
  • Handles any data types as function parameters
  • Uses === strict equality for input comparison

The challenge lies in creating an efficient caching mechanism that works with any combination of argument types while maintaining optimal performance.

Input & Output

example_1.js โ€” Basic Number Function
$ Input: let callCount = 0; const memoizedFn = memoize(function (a, b) { callCount += 1; return a + b; }); memoizedFn(2, 3); // 5 memoizedFn(2, 3); // 5 console.log(callCount); // 1
โ€บ Output: 1
๐Ÿ’ก Note: The function is only called once. The second call with identical arguments (2, 3) returns the cached result without executing the original function.
example_2.js โ€” Object Arguments
$ Input: let callCount = 0; const memoizedFn = memoize(function (a) { callCount += 1; return a.value * 2; }); const obj = {value: 5}; memoizedFn(obj); // 10 memoizedFn(obj); // 10 console.log(callCount); // 1
โ€บ Output: 1
๐Ÿ’ก Note: Even with object arguments, the memoization works correctly using reference equality. The same object reference triggers cache hit.
example_3.js โ€” Different Arguments
$ Input: let callCount = 0; const memoizedFn = memoize(function (a, b) { callCount += 1; return a * b; }); memoizedFn(2, 3); // 6 memoizedFn(3, 2); // 6 console.log(callCount); // 2
โ€บ Output: 2
๐Ÿ’ก Note: Different argument combinations (2,3) vs (3,2) are treated as separate cache entries, so the function is called twice.

Constraints

  • The function can accept 0 to 10 arguments
  • Arguments can be of any type: primitives, objects, arrays, functions
  • The function can return any type of value
  • Inputs are considered identical only if they are === equal
  • Cache size is unlimited - no automatic cleanup

Visualization

Tap to expand
Memoization: Before vs AfterWithout MemoizationCall fn(5)ComputeHeavy WorkReturn 25Call fn(5)ComputeAGAIN! ๐Ÿ˜žReturn 25Total Time: 200msWith MemoizationCall fn(5)Compute +CacheReturn 25Call fn(5)CacheHit! โšกReturn 25Total Time: 101msCache Structure (Map)Key: "5"โ†’Value: 25O(1) lookup time - instant results!๐Ÿš€ 98% faster for repeated calls!
Understanding the Visualization
1
First Call
Function executes normally, result stored in cache
2
Cache Storage
Arguments become cache key, result becomes cached value
3
Subsequent Calls
Same arguments trigger cache hit, return instant result
4
Performance Boost
Expensive computations avoided, dramatic speed improvement
Key Takeaway
๐ŸŽฏ Key Insight: Memoization trades memory for speed, converting expensive repeated computations into instant cache lookups using hash map optimization.
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
58.9K Views
High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen