Created
November 17, 2019 12:48
-
-
Save trinadhkoya/d454c82f84f325de6ae1ea90e88965ce to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/siceyak
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
const multiplication = (n) => (n)*(n-1); | |
const memoizationDemo = (fn) => { | |
console.log(fn) | |
let cache = {}; | |
return (...args) => { | |
let ip = args[0]; | |
if(ip in cache) { | |
console.log("Value access from cache") | |
return cache[ip] | |
}else{ | |
console.log("Value was not stored in cache "); | |
let res = fn(ip); | |
cache[ip]=res; | |
return res; | |
} | |
} | |
}; | |
const returnMultiplication = memoizationDemo(multiplication); | |
console.log(returnMultiplication(10)); | |
console.log(returnMultiplication(11)); | |
console.log(returnMultiplication(10)); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">const multiplication = (n) => (n)*(n-1); | |
const memoizationDemo = (fn) => { | |
console.log(fn) | |
let cache = {}; | |
return (...args) => { | |
let ip = args[0]; | |
if(ip in cache) { | |
console.log("Value access from cache") | |
return cache[ip] | |
}else{ | |
console.log("Value was not stored in cache "); | |
let res = fn(ip); | |
cache[ip]=res; | |
return res; | |
} | |
} | |
}; | |
const returnMultiplication = memoizationDemo(multiplication); | |
console.log(returnMultiplication(10)); | |
console.log(returnMultiplication(11)); | |
console.log(returnMultiplication(10));</script></body> | |
</html> |
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
const multiplication = (n) => (n)*(n-1); | |
const memoizationDemo = (fn) => { | |
console.log(fn) | |
let cache = {}; | |
return (...args) => { | |
let ip = args[0]; | |
if(ip in cache) { | |
console.log("Value access from cache") | |
return cache[ip] | |
}else{ | |
console.log("Value was not stored in cache "); | |
let res = fn(ip); | |
cache[ip]=res; | |
return res; | |
} | |
} | |
}; | |
const returnMultiplication = memoizationDemo(multiplication); | |
console.log(returnMultiplication(10)); | |
console.log(returnMultiplication(11)); | |
console.log(returnMultiplication(10)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment