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

simple-lru

v0.0.3

Published

minimal Least Recently Used Cache

Downloads

813

Readme

Simple LRU cache in JavaScript

browser support

The implementation is inspired by node-lru-cache by Isaac Schlueter. The motivation of this project is to provide Object.create fallback in order to work on IE8.

Installation

With npm

$ npm install simple-lru

With Component

$ component install smagch/simple-lru

With Bower

$ bower install simple-lru

Or download tarball.

Examples

You can set any value with a string key.

var cache = new SimpleLRU(3);
cache.set('a', 'A');
cache.set('b', {name: 'smagch'});
cache.set('c', 1000);
var a = cache.get('a'); // a = 'A'
var b = cache.get('b'); // b = {name: 'smagch'}
var c = cache.get('c'); // c = 1000

It removes cache items automatically when total length get out of max.

var cache = new SimpleLRU(3);
cache.set('a', 'A');
cache.set('b', 'B');
cache.set('c', 'C');
cache.set('d', 'D');
var a = cache.get('a'); // a = undefined;
var b = cache.get('b'); // b = 'B'
var c = cache.get('c'); // c = 'C'
var d = cache.get('d'); // d = 'D'
var keys = cache.keys(); // keys = ['b', 'c', 'd']

Since it's using LRU cache algorithm, it removes the least recently used item.

var cache = new SimpleLRU(3);
cache.set('a', 'A');
cache.set('b', 'B');
cache.set('c', 'C');
// Calling `get` with 'a', 'a' is the most recently used item.
var a = cache.get('a'); // a = 'A'
var keys = cache.keys(); // keys = ['b', 'c', 'a']

cache.set('d', 'D');
a = cache.get('a'); // a = 'A'
var b = cache.get('b'); // b = undefined
var c = cache.get('c'); // c = 'C'
var d = cache.get('d'); // d = 'D'

API

new SimpleLRU(max)

Create a new SimpleLRU instance with given max cache number. max option should be a positive number.

SimpleLRU#set(key, value)

Set a new cache with given key and value.

SimpleLRU#get(key)

Get a cache by key.

SimpleLRU#peek(key)

Get a cache without updating recently used order.

SimpleLRU#del(key)

Delete a cache by key.

SimpleLRU#has(key)

See if it has a cache with given key.

SimpleLRU#length()

Return total number of cache items.

SimpleLRU#reset()

Clear all stored cache.

SimpleLRU#keys()

Return an Array of existing cache keys in least recently used order. The most recently used cache item will be the last.

SimpleLRU#max([max])

Getter|Setter of max option. Get a max option if it has no argument. And change max option with an argument. It will trim cache when new max option is smaller than its length.

License

MIT