memoizeOne
takes a function as input and returns a memoized version of it.
const memoizedFunction = memoizedOne(() => {})
If the current invocation is the same as the last one then a cached result is returned.
function add(a, b) {
return a + b;
}
const memoizedAdd = memoizeOne(add);
memoizedAdd(1, 2);
// add function: is called
// [new value returned: 3]
memoizedAdd(1, 2);
// add function: not called because same input as last time
// [cached result is returned: 3]
memoizedAdd(2, 3);
// add function: is called
// [new value returned: 5]
memoizedAdd(2, 3);
// add function: not called
// [cached result is returned: 5]
memoizedAdd(1, 2);
// add function is called again as only the last invocation is cached
// [result is returned: 3]
Your task is to implement the memoizeOne function such that all the test cases pass.
- npm install
- npx jest
Click here for more details
Changes in the input invalidates the cache and calls the function again. this
is also treated as an input. In other words, if this
changes the function will be called again.
function add(a, b) {
return a + b;
}
const memoizedAdd = memoizeOne(add);
memoizedAdd.call({}, 1, 2);
// add function: is called with {}
// [new value returned: 3]
memoizedAdd.call({ a: 1 }, 1, 2);
// add function: is called with { a: 1 }
// [new value returned: 3]