https://github.com/webreflection/basic-lru
A fast and lightweight Map based LRU implementation.
https://github.com/webreflection/basic-lru
Last synced: 10 months ago
JSON representation
A fast and lightweight Map based LRU implementation.
- Host: GitHub
- URL: https://github.com/webreflection/basic-lru
- Owner: WebReflection
- License: isc
- Created: 2020-02-04T18:10:27.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-02-07T09:38:00.000Z (over 6 years ago)
- Last Synced: 2025-04-14T00:11:26.769Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 19.5 KB
- Stars: 21
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# basic-lru
**Social Media Photo by [Mr Cup / Fabien Barral](https://unsplash.com/@iammrcup) on [Unsplash](https://unsplash.com/)**
[](https://travis-ci.com/WebReflection/basic-lru) [](https://coveralls.io/github/WebReflection/basic-lru?branch=master)
A fast and lightweight, as in [684 bytes](https://unpkg.com/basic-lru), Map based LRU implementation.
```js
import LRU from 'basic-lru';
const LRU = require('basic-lru');
// https://unpkg.com/basic-lru to have LRU globally
// new LRU(maxSize)
const lru = new LRU(100);
// new LRU({max}) or new LRU({maxSize})
const lru = new LRU({max: 1000});
// new LRU({maxAge}) in milliseconds
const lru = new LRU({maxAge: 1000});
// variants
const lru = new LRU({max: 100, maxAge: 1000});
const lru = new LRU({maxSize: 100, maxAge: 1000});
```
### About
This module is a drop-in replacement for any [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) instance, and it's mostly 100% compatible with [lru](https://github.com/chriso/lru), [lru-cache](https://github.com/isaacs/node-lru-cache), [quick-lru](https://github.com/sindresorhus/quick-lru), and [lru-map](https://github.com/bchociej/lru-map) modules.
Differently from other modules, this one has the least amount of LOC, zero dependencies, and it's based on _ES2015_ class capability to extend the native `Map`.
### The Map Extend Differences
The only difference from a real Map, beside implementing a classic LRU cache, is the `peek(key)` method, to access an entry without flagging its access time anyhow, and borrowed from other libraries, plus a constructor, also borrowed from other libraries, that accepts either an integer, or an options object with `max`, or `maxSize`, and a `maxAge` property, where all of these are optional too.