implement memoizeOne()
2022年5月1日小于 1 分钟
implement memoizeOne()
Question
In problem 14. Implement a general memoization function, you are asked to implement a memo function without space concern.
But in reality, it could be a problem if cache bloats.
You might need to restrict the cache capacity, just like memoize-one , it only remembers the latest arguments and result.
Please implement your own memoizeOne()
, it takes 2 arguments
- target function
- (optional) a equality check function to compare current and last arguments
Default equality check function should be a shallow comparison on array items with strict equal ===
.
Code
/**
* @param {Function} func
* @param {(args: any[], newArgs: any[]) => boolean} [isEqual]
* @returns {any}
*/
function memoizeOne(func, isEqual) {
// your code here
}