Created
May 17, 2016 15:38
-
-
Save louisremi/ed1f8357642be8ecc4a88a78e4fd9870 to your computer and use it in GitHub Desktop.
Last Result Used
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
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(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Why are you re-setting the value on the map, lines 22-23?