Problem: Cache Expensive Function Calls via Proxy

hard
40 min
Intercept function calls and cache results based on input arguments.

Problem statement

Your analytics module runs expensive computations on input data. Repeated calls with the same arguments waste CPU time because the function recalculates the same result repeatedly.

You want to use a Proxy to automatically cache results for identical arguments, so repeated calls are served instantly from memory. This will simulate how memoization or API response caching can be achieved transparently—without changing the original function logic.

Goal

Create a function createCachedFunction(fn) that returns a Proxy wrapping fn. The Proxy should intercept calls, check for cached results, and return them immediately if available.

Constraints

  • You must use a Proxy with the apply trap.

  • Cache results based on stringified arguments (JSON.stringify(args)).

  • Do not modify the original function or wrap it manually.

  • Only recompute results if the input arguments differ.

In JavaScript’s Proxy API, the apply trap is triggered whenever the proxy is called, much like a function.
It receives three parameters:

  • target: The original function being called.

  • thisArg: The value of this inside the call.

  • args: The array of arguments passed to the function.

You can use these parameters to intercept or modify how the function executes before returning a result.

Sample output

The examples below illustrate what the output should look like:

const cachedCompute = createCachedFunction(heavyComputation);
console.log(cachedCompute(2, 3));
console.log(cachedCompute(2, 3)); // should return cached result
/* Expected output:
Computing...
<some number>
<same number as above>
*/
console.log(cachedCompute(4, 5)); // new computation
console.log(cachedCompute(4, 5)); // cached again
/* Expected output:
Computing...
<some number>
<same number as above>
*/

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Problem: Cache Expensive Function Calls via Proxy

hard
40 min
Intercept function calls and cache results based on input arguments.

Problem statement

Your analytics module runs expensive computations on input data. Repeated calls with the same arguments waste CPU time because the function recalculates the same result repeatedly.

You want to use a Proxy to automatically cache results for identical arguments, so repeated calls are served instantly from memory. This will simulate how memoization or API response caching can be achieved transparently—without changing the original function logic.

Goal

Create a function createCachedFunction(fn) that returns a Proxy wrapping fn. The Proxy should intercept calls, check for cached results, and return them immediately if available.

Constraints

  • You must use a Proxy with the apply trap.

  • Cache results based on stringified arguments (JSON.stringify(args)).

  • Do not modify the original function or wrap it manually.

  • Only recompute results if the input arguments differ.

In JavaScript’s Proxy API, the apply trap is triggered whenever the proxy is called, much like a function.
It receives three parameters:

  • target: The original function being called.

  • thisArg: The value of this inside the call.

  • args: The array of arguments passed to the function.

You can use these parameters to intercept or modify how the function executes before returning a result.

Sample output

The examples below illustrate what the output should look like:

const cachedCompute = createCachedFunction(heavyComputation);
console.log(cachedCompute(2, 3));
console.log(cachedCompute(2, 3)); // should return cached result
/* Expected output:
Computing...
<some number>
<same number as above>
*/
console.log(cachedCompute(4, 5)); // new computation
console.log(cachedCompute(4, 5)); // cached again
/* Expected output:
Computing...
<some number>
<same number as above>
*/

Good luck trying the problem! If you’re unsure how to proceed, check the “Solution” tab above.

Node.js
// Simulated expensive computation
function heavyComputation(x, y) {
console.log('Computing...');
return x * y + Math.random(); // random makes recomputation visible
}
// Your code here
// Example usage
const cachedCompute = createCachedFunction(heavyComputation);
console.log(cachedCompute(2, 3));
console.log(cachedCompute(2, 3)); // should return cached result
/* Expected output:
Computing...
<some number>
<same number as above>
*/
console.log(cachedCompute(4, 5)); // new computation
console.log(cachedCompute(4, 5)); // cached again
/* Expected output:
Computing...
<some number>
<same number as above>
*/