Skip to content

Instantly share code, notes, and snippets.

@louisremi
Created May 17, 2016 15:38
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save louisremi/ed1f8357642be8ecc4a88a78e4fd9870 to your computer and use it in GitHub Desktop.
Save louisremi/ed1f8357642be8ecc4a88a78e4fd9870 to your computer and use it in GitHub Desktop.
Last Result Used
function LRU(limit) {
this.limit = limit || 1000;
this.map = new Map();
}
LRU.prototype.has = function(key) {
return this.map.has(key);
}
LRU.prototype.set = function(key, value) {
this.map.delete(key);
this.map.set(key, value);
if ( this.map.size > this.limit ) {
this.map.delete( this.map.keys().next().value );
}
return this;
};
LRU.prototype.get = function(key) {
if ( this.map.has(key) ) {
var value = this.map.get(key);
this.map.delete(key);
this.map.set(key, value);
return value;
}
};
LRU.prototype.clear = function() {
this.map.clear();
};
@pavlelekic
Copy link

Why are you re-setting the value on the map, lines 22-23?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment