Last active
June 16, 2017 13:27
-
-
Save thephilip/5ca17c9e55723094a0c0b460e4aee72f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Function object extension to add memoization | |
* to any single-argument function. | |
*/ | |
Function.prototype.memoized = function () { | |
let key = JSON.stringify(arguments); | |
this._cache = this._cache || {}; | |
this._cache[key] = this._cache[key] || | |
this.apply(this, arguments); | |
return this._cache[key]; | |
}; | |
Function.prototype.memoize = function () { | |
let fn = this; | |
if (fn.lenth === 0 || fn.length > 1) { | |
return fn; | |
} | |
return function () { | |
return fn.memoized.apply(fn, arguments); | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment