npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

modern-lru

v1.4.0

Published

LRU cache on modern javascript

Downloads

77

Readme

LRU cache for node.js

npm Travis Coverage

Simple O(1) LRU cache implementation based on native Map class.

Installation

npm i modern-lru --save

API

LRU cache class is based on native Map so API is the same. Also you can safely use anything for keys, for example undefined, NaN or some object pointer.

Constructor

new LRU(limit[, iterable])

limit - cache keys limit, positive integer.

iterable - optional Array or other iterable object whose elements are key-value pairs (arrays with two elements, e.g. [[ 1, 'one' ],[ 2, 'two' ]]). Each key-value pair is added to the new LRU instance.

Properties

.size

The number of keys currently in the cache.

.limit

Cache keys limit in current instance

Methods

.clear()

Removes all key/value pairs from the LRU object.

.delete(key)

Removes any value associated to the key and returns the value that .has(key) would have previously returned. .has(key) will return false afterwards.

.entries()

Returns a new Iterator object that contains an array of [key, value] for each element in the LRU object in last usage order.

.forEach(callbackFn[, thisArg])

Calls callbackFn once for each key-value pair present in the LRU object, in last usage order. If a thisArg parameter is provided to forEach, it will be used as the this value for each callback.

.get(key)

Returns the value associated to the key, or undefined if there is none.

.has(key)

Returns a boolean asserting whether a value has been associated to the key in the LRU object or not.

.keys()

Returns a new Iterator object thet contains the keys for each element in the LRU object in last usage order.

.set(key, value)

Sets the value for the key in the LRU object. Returns the LRU object.

.values()

Returns a new Iterator object that contains the values for each element in the LRU object in last usage order.

[@@iterator]()

Returns a new Iterator object that contains an array of [key, value] for each element in the LRU object in last usage order.

Example

const LRU = require('./');

// lru cache with limit of 3 entries
const cache = new LRU(3);

console.log(cache.limit); // 3
console.log(cache.size); // 0 (elements count)

cache.set('first', 'first');
cache.set('second', 'second');
cache.set('third', 'third');

console.log(cache.get('second')); // second
console.log(cache); // LRU { 'second' => 'second', 'third' => 'third', 'first' => 'first' }

cache.set('fourth', 'fourth');
console.log(cache.has('first')); // false ("first" was evicted)
console.log(cache.get('fourth')); // fourth

// also it implements default Map
console.log(cache instanceof Map); // true

// so you can use `keys()` for example
console.log(Array.from(cache.keys()).join(', ')); // 'fourth, second, third'

// or use objects, as with Map
const myObject = { test: 5 };
cache.set(myObject, 'testme');
console.log(cache.has(myObject)); // true
console.log(cache.get(myObject)); // 'testme'
console.log(cache.get({ test: 5})); // undefined (different pointer)

cache.clear();

// also `undefined` and `NaN` are supported, as with Map
cache.set(undefined, 5);
cache.set(NaN, 10);
console.log(cache); // LRU { NaN => 10, undefined => 5 }

Alternatives

lru-fast - classic doubly-linked list based lru, superfast and memory efficient.

quick-lru - a little bit dirty with broken keys order, but supersimple and fast alternative, inspired by the hashlru algorithm.

Also some benchmarks here with more packages, but with no feature comparison.