Created
March 4, 2011 17:39
-
-
Save cburgdorf/855338 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.prototype.cache = function () { | |
if (this.cache === undefined){ | |
this.cache = {}; | |
} | |
if (this.cache[arguments[0]] !== undefined){ | |
console.log("using the cache"); | |
return this.cache[arguments[0]]; //I wonder why this works even for object arguments (e.g. {test: 3})? Implicity converted to string? | |
} | |
else{ | |
console.log("running method"); | |
return this.cache[arguments[0]] = this.call(this, arguments[0]); | |
} | |
}; | |
function isPrime( num ) { | |
// everything but 1 can be prime | |
var prime = num != 1; | |
for ( var i = 2; i < num; i++ ) { | |
if ( num % i == 0 ) { | |
prime = false; | |
break; | |
} | |
} | |
return prime; | |
} | |
console.log(isPrime.cache(524287)); | |
console.log(isPrime.cache(524287)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment