Skip to content

Instantly share code, notes, and snippets.

@trinadhkoya
Created November 17, 2019 12:48
Show Gist options
  • Save trinadhkoya/d454c82f84f325de6ae1ea90e88965ce to your computer and use it in GitHub Desktop.
Save trinadhkoya/d454c82f84f325de6ae1ea90e88965ce to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/siceyak
<!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>
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