mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-19 18:13:44 +08:00
30 lines
593 B
JavaScript
30 lines
593 B
JavaScript
/**
|
|
* Memoize
|
|
* @param {Function} fn
|
|
* @returns
|
|
*/
|
|
export const memoize = (func) => {
|
|
// eslint-disable-next-line no-console
|
|
console.log(`Creating cache for function '${func.name}'`)
|
|
|
|
const cache = {}
|
|
|
|
return (...args) => {
|
|
const [arg] = args
|
|
|
|
if (arg in cache) {
|
|
// eslint-disable-next-line no-console
|
|
console.log(`Reading cache with argument ${arg}`)
|
|
|
|
return cache[arg]
|
|
}
|
|
|
|
// eslint-disable-next-line no-console
|
|
console.log(`Updating cache with argument ${arg}`)
|
|
|
|
const result = func(arg)
|
|
cache[arg] = result
|
|
return result
|
|
}
|
|
}
|