Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
another version of general cache "" which supports "this" object for callee
var stats = {
hit: 0, miss: 0
exports.LRU = function(size,k_match) {
var tbl=[];
if (!k_match) k_match=function(k1,k2) {
if (k1.length!=k2.length)
return false;
for (var i=0; i<k1.length; i++)
if (k1[i]!==k2[i]) return false;
return true;
return function() {
var k=[];
var f=k.shift();
var t=k.shift();
for (var i=tbl.length-1; i>=0; i--) {
var r=tbl[i];
if (r.f===f && r.t===t && k_match(r.k,k)) {
while (i<tbl.length-1) {
tbl[i]=tbl[i+1]; i++;
return r.v;
var v=f.apply(t,k);
if (tbl.length>=size) tbl.shift();
return v;
exports.stats = stats;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment