# map-lru

> "Least Recently Used" (LRU) cache compatible to ES6 Map

Latest version **2.1.0** (published 2024-08-14) · Unlicense license · 0 weekly downloads

## Install

```sh
npm install map-lru
pnpm add map-lru
yarn add map-lru
bun add map-lru
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2024-08-14 |
| First published | 2017-12-09 |
| Weekly downloads | 0 |
| License | Unlicense |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=12 |
| Dependencies | 0 |
| Unpacked size | 20.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | commenthol |
| Maintainers | commenthol |
| Keywords | cache, caching, fast, least, lru, map, recently, used |

## Links

- npm: https://www.npmjs.com/package/map-lru
- Repository: https://github.com/commenthol/map-lru
- Homepage: https://github.com/commenthol/map-lru#readme
- Issues: https://github.com/commenthol/map-lru/issues
- npm.io page: https://npm.io/package/map-lru

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2024-08-14
- 2.0.0 — 2021-09-14
- 2.0.0-0 — 2021-09-12
- 1.1.0 — 2019-07-23
- 1.0.2 — 2018-07-21
- 1.0.1 — 2018-07-09
- 1.0.0 — 2017-12-09

## README

# MapLRU

> "Least Recently Used" (LRU) cache compatible to ES6 Map

[![NPM version](https://badge.fury.io/js/map-lru.svg)](https://www.npmjs.com/package/map-lru/)
[![Build Status](https://app.travis-ci.com/commenthol/map-lru.svg?branch=master)](https://app.travis-ci.com/commenthol/map-lru)
[![Coverage Status](https://coveralls.io/repos/github/commenthol/map-lru/badge.svg?branch=master)](https://coveralls.io/github/commenthol/map-lru?branch=master)

Useful for caching with limited memory usage.
API compatible with built-in [Map][] object.

## Install

```
$ npm install map-lru
```

## Usage

```js
import MapLRU from 'map-lru' // ES5
// const MapLRU = require('map-lru') // commonJs
const cache = new MapLRU(10)

cache.set('♥', '♥♥♥')

cache.has('♥');
//=> true
cache.get('♥');
//=> '♥♥♥'
cache.last
//=> '♥'
cache.size
//=> 1
```


## API

### new MapLRU(maxSize)

Creates a new instance

**Parameters**

- `maxSize` **[Number](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number)** max. size of the LRU cache.

----

_Additional methods_

- `last`

  Returns the last accessed key.

  Returns **Any**

- `peek(key)`

  Get an item without marking it as recently used.

  **Parameters**

  - `key` **Any**

- `keysAccessed()`

  keys in order of access - last one is most recently used one.

  Returns **Iterator** Iterator object

----

_Default [Map][] methods_

- [`size`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/size)

- [`get(key)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get)

- [`set(key, value)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/set)

- [`has(key)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has)

- [`delete(key)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/delete)

- [`clear()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/clear)

- [`keys()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/keys)

- [`values()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/values)

- [`entries()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/entries)

- [`forEach()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/forEach)

- [`[Symbol.iterator]`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set/@@iterator)

## License

[Unlicense](https://unlicense.org)

[Map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

---
_Source: https://npm.io/package/map-lru · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
