Last active
December 11, 2018 18:20
-
-
Save pramodmg/c4a0e41a2d9a17583fa855b94c673374 to your computer and use it in GitHub Desktop.
Implementation of the memory Cache using the javascript.
This file contains hidden or 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
/* | |
using the memCache using the javascript | |
1. we are implementing to reduce the response time, no other data space is being used. | |
2. Better approach to solve most of the clientside optimizaition. | |
*/ | |
function memCache(func){ | |
var cache = {}; | |
return function(){ | |
var key = JSON.stringify(arguments); | |
if (cache[key]){ | |
return cache[key]; | |
} else { | |
// call the fucnction with arguments and store the cache[key[ to val. | |
val = func.apply(null, arguments); | |
cache[key] = val; | |
return val; | |
} | |
} | |
}; | |
// example using the factorial | |
var fib = memCache(function(n) { | |
console.log("the val of n is",n); | |
if(n>2){ | |
return 1; | |
} else { | |
console.log("loading will be seen only before the value will be not cached..."); | |
return fib(n-2) + fib(n-1); | |
} | |
}); | |
fib(10); | |
console.log('------'); | |
fib(10); | |
// actual output. | |
the val of n is 10 | |
loading will be seen only before the value will be not cached... | |
the val of n is 8 | |
loading will be seen only before the value will be not cached... | |
the val of n is 6 | |
loading will be seen only before the value will be not cached... | |
the val of n is 4 | |
loading will be seen only before the value will be not cached... | |
the val of n is 2 | |
loading will be seen only before the value will be not cached... | |
the val of n is 0 | |
the val of n is 1 | |
the val of n is 3 | |
loading will be seen only before the value will be not cached... | |
{ '{"0":0}': 1, '{"0":1}': 1, '{"0":2}': 2 } | |
{ '{"0":0}': 1, '{"0":1}': 1, '{"0":2}': 2 } | |
the val of n is 5 | |
loading will be seen only before the value will be not cached... | |
{ '{"0":0}': 1, | |
'{"0":1}': 1, | |
'{"0":2}': 2, | |
'{"0":3}': 3, | |
'{"0":4}': 5 } | |
{ '{"0":0}': 1, | |
'{"0":1}': 1, | |
'{"0":2}': 2, | |
'{"0":3}': 3, | |
'{"0":4}': 5 } | |
the val of n is 7 | |
loading will be seen only before the value will be not cached... | |
{ '{"0":0}': 1, | |
'{"0":1}': 1, | |
'{"0":2}': 2, | |
'{"0":3}': 3, | |
'{"0":4}': 5, | |
'{"0":5}': 8, | |
'{"0":6}': 13 } | |
{ '{"0":0}': 1, | |
'{"0":1}': 1, | |
'{"0":2}': 2, | |
'{"0":3}': 3, | |
'{"0":4}': 5, | |
'{"0":5}': 8, | |
'{"0":6}': 13 } | |
the val of n is 9 | |
loading will be seen only before the value will be not cached... | |
{ '{"0":0}': 1, | |
'{"0":1}': 1, | |
'{"0":2}': 2, | |
'{"0":3}': 3, | |
'{"0":4}': 5, | |
'{"0":5}': 8, | |
'{"0":6}': 13, | |
'{"0":7}': 21, | |
'{"0":8}': 34 } | |
{ '{"0":0}': 1, | |
'{"0":1}': 1, | |
'{"0":2}': 2, | |
'{"0":3}': 3, | |
'{"0":4}': 5, | |
'{"0":5}': 8, | |
'{"0":6}': 13, | |
'{"0":7}': 21, | |
'{"0":8}': 34 } | |
------ | |
{ '{"0":0}': 1, | |
'{"0":1}': 1, | |
'{"0":2}': 2, | |
'{"0":3}': 3, | |
'{"0":4}': 5, | |
'{"0":5}': 8, | |
'{"0":6}': 13, | |
'{"0":7}': 21, | |
'{"0":8}': 34, | |
'{"0":9}': 55, | |
'{"0":10}': 89 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment