Problem: Cache Expensive Function Calls via Proxy
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
applytrap.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 ofthisinside 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 computationconsole.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
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
applytrap.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 ofthisinside 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 computationconsole.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.