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

lru-js

v0.4.0

Published

simple LRU cache implementation in nodejs

Downloads

5

Readme

js-last-recently-used-cache

simple LRU cache implementation in javascript. It uses a doubly linked list (instance.list) and a hash map (instance.map) to get/add/remove cache elements with O(1) time complexity.

Install

$ npm install --save js-last-recently-used-cache

Usage

import LRY from 'js-last-recently-used-cache'

const cache = new LRU({limitSize: 3});
cache.set('1', 'one')
cache.set('2', 'two')
cache.set('3', 'three')
cache.get('3') // 'three'
cache.get('1') // 'one'
cache.set('4', 'four');
cache.get('4') //four
cache.has('1') // false (bumped out since cache has a size limit of 3)

API

Table of Contents

LRU

Parameters

  • params.limitSize Number size limit (in number of cached elements) (optional, default {})
    • params.limitSize.limitSize (optional, default 100)

has

check if cache contains specific key

Parameters

  • key any to check if in cache

Returns Boolean whether or not key is in cache

set

set new data into the cache

Parameters

  • args ...any
  • key/value (Array<any, any> | Array<Array<any, any>>) pair(s) to store in cache

Returns (Array<Node> | Node) node(s) inserted in doubly linked list

get

Get the data stored in cache by its key

Parameters

  • key any identifier of the data to get from cache

Returns any data stored in cache for specified key

size

gets the number of items in the cache

Returns Number

delete

delete a specific value from the cache by its key (delets it from the hashmap and the doubly linked list)

Parameters

  • key any to delete from cache

mostRecentKey

Gets the identifier (key) of the last recently used key/value pair stored in cache

Returns any

leastRecentKey

Gets the identifier (key) of the least recently used key/value pair stored in cache

Returns any

mostRecentValue

Gets most recently used value stored in cache

Returns any

leastRecentValue

Gets most recently used value stored in cache

Returns any

mostRecent

Gets most recently used key/value pair stored in cache

Returns Array<any, any> array with key of cached data at index 0 and value at index 1

leastRecent

Gets least recently used key/value pair stored in cache

Returns Array<any, any> array with key of cached data at index 0 and value at index 1

pop

pop the least recently used key/value in the cache (pops the last element of the double linked list and removes it from hashmap)

bump

bump a specific cached data to last recently used (adds it to front of the double linked list list)

Parameters

  • key any key of the data to bump

Returns Node new node created at the front of the list with the same cached data

bumpNode

bump a specific node to last recently used (front of the linked list)

Parameters

  • node Node node to bump to front of list

Returns Node new node placed at front of list with data of passed node (passed nodeis removed)

setSinglePair

set a single key/value pair in the cache

Parameters

  • key any to identify the data with
  • value any value to store in cache

Returns Node node created in doubly linked list with data property being an array of [key,value]

getNode

gets the double linked list's node corresponding to specific key

Parameters

  • key any cached element key

Returns Node

getNodeValue

gets the value of a specific node from the doubly linked list (gets the second index of the node's data)

Parameters

  • node Node node in doubly linked list this.list

Returns any cached value

getNodeKey

gets the key of a specific node from the doubly linked list (gets the second index of the node's data)

Parameters

  • node Node node in doubly linked list this.list

Returns any cached key

License

MIT © Guillaume