Last active
June 14, 2016 16:47
-
-
Save Ynote/10e6b85e7887b3769690 to your computer and use it in GitHub Desktop.
[Javascript] - Power of two memozing function with closure
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.prototype.memoized = function(key) { | |
this._values = this._values || {}; | |
return this._values[key] !== undefined ? | |
this._values[key] : | |
this._values[key] = this.apply(this, arguments); | |
} | |
Function.prototype.memoize = function() { | |
var fn = this; | |
return function() { | |
fn.memoized.apply(fn, arguments); | |
} | |
} | |
var isPowerOfTwo = (function(n) { | |
return !(n & (n - 1)); // binary operator | |
}).memoize(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment