2.0.0 • Published 7 years ago

faster-lru-cache v2.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

Build Status

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.

BrowserAdvantage 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

import LRUCache from "faster-lru-cache";

const cache = new LRUCache(3);

cache.set("foo", "1234");
cache.set("bar", "abcde");

cache.get("foo"); // returns "1234"

cache.set("baz", "asdf");

// 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.set("whatever", "test");

console.log(cache);
// 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.