Created
March 8, 2013 17:04
-
-
Save radvieira/5118005 to your computer and use it in GitHub Desktop.
js master class
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.cached = function() { | |
var cache = {}, | |
that = this; | |
return function(arg) { | |
if(!(arg in cache)) { | |
cache[arg] = that(arg); | |
} | |
return cache[arg]; | |
}; | |
}; | |
var cachedSin = Math.sin.cached(); | |
console.log(cachedSin(1)); | |
console.log(cachedSin(1)); | |
var isPrime = function(num) { | |
var prime = num != 1; | |
for ( var i = 2; i < num; i++ ) { | |
if ( num % i == 0 ) { | |
prime = false; | |
break; | |
} | |
} | |
return prime; | |
}; | |
var cachedIsPrime = isPrime.cached(); | |
console.log(cachedIsPrime(524287)); | |
console.log(cachedIsPrime(9369319)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment