Exercise on Proxies
Explore how to apply JavaScript proxies in practical exercises. Learn to track function calls in recursive Fibonacci calculations, optimize performance with memoization using proxy-based lookup tables, and implement revocable proxies to manage timed discounts on object properties.
We'll cover the following...
Exercise 1:
Suppose the following fibonacci implementation is given:
fibonacci = n =>
n <= 1 ? n :
fibonacci( n - 1 ) + fibonacci( n - 2 );
Determine how many times the fibonacci function is called when evaluating fibonacci( 12 ).
Determine how many times fibonacci is called with the argument 2 when evaluating fibonacci( 12 ).
Explanation:
The solution is not that hard. As we learned earlier,
fibonacci = new Proxy( fibonacci, { uses the same reference name as our fibonacci function so that the proxy can be applied on all recursive calls as well.
Within the proxy, we increment ...