Faster LRU-Cache
Faster LRU-Cache is a really small implementation of an LRU-Cache built on ES6
Maps
. The primary goal was to keep it as small as possible for usage in web
apps, where size is critical for performance.
When writing it I did a few simple micro-benchmarks on esbench
which shows that Maps
are a faster than normal Objects
in modern browsers.
Browser | Advantage over Object |
---|---|
Chrome 54 | +1.6% (this is neglectable) |
Safari 10.1 | +40% |
Firefox 50 | +50% |
Keep in mind though that the result of micro-benchmarks may change drastically over time as browser vendors keep optimizing their engines.
Note: All benchmarks are run on a Macbook Pro 15" 2015
Usage
; const cache = 3; cache;cache; cache; // returns "1234" cache; // We have 3 items in the cache now and reached our limit.// These items are "foo", "bar", "baz".// The next insertion will remove oldest item in the cache.// Note that this is NOT "foo", because we accessed it previously// which is why it is marked as newer than "bar". The latter will be removed.cache; console;// Logs:// LRUCache {// _cache: Map { 'foo' => '1234', 'baz' => 'asdf', 'whatever' => 'test' },// usage: [ 'whatever', 'baz', 'foo' ],// limit: 3 }
API
constructor(limit: number)
Set the maximum number of items that should be present in the cache. The
default is 0
which means there is no limit.
cache.set(key: any, value: any)
Add an item to the cache.
cache.get(key: any)
Retrieve an item from the cache.
cache.clear()
Clear the cache.
cache.size: number
Get the current cache size.