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

@wunderwerk/lru-map

v0.2.1

Published

Map based on Least Recently Used algorithm.

Downloads

9

Readme

LRU Map implementation

This package provides a Map that implements the LRU (Least Recently Used) algorithm.

Most of the implementation was inferred from rsms/js-lru.

Design

  • Map entry priority is achieved by linking each entry to the newer and older sibling in the list.
  • The map has a head (oldest) and a tail (newest).
  • When an entry is retrieved from the list, it is automatically the newest entry.
  • The map can be serialized to JSON and created from previously serialized data. This makes the map persistable in the browser.

General design:

           entry             entry             entry             entry
           ______            ______            ______            ______
          | head |.newer => |      |.newer => |      |.newer => | tail |
.oldest = |  A   |          |  B   |          |  C   |          |  D   | = .newest
          |______| <= older.|______| <= older.|______| <= older.|______|

       removed  <--  <--  <--  <--  <--  <--  <--  <--  <--  <--  <--  added

Flavors

The LRU Map is currently implemented in two flavors:

  • Limit based A limit of the maximum number of entries the LRU map can hold defines the max number of valid entries. When a new entry is added to the map, the oldest entry is removed.
  • Entry-Size based Each entry must have a size associated with it (e.g. bytes) when adding them to the map. The map has a maximum size that must not be exceeded. When a new entry is added to the map, the oldest entry/entries are removed until the new entries size fits into the map.

Examples

Limit based

import { LimitBasedLRUMap } from '@wunderwerk/lru-map';

const map = new LimitBasedLRUMap<string, string>(3);

map.set('one', { value: 'value-one' });
map.set('two', { value: 'value-two' });
map.set('three', { value: 'value-three' });

map.toString();     // -> "one < two < three";

// By getting 'two', it is now the most recently used entry.
map.get('two');

map.toString();     // -> "one < three < two";

// By adding a new entry and therefore exceeding the map limit of 3,
// The least recently used entry is removed.
map.set('four', { value: 'value-four' });

map.toString();     // -> "three < two < four";

Size based

import { SizeBasedLRUMap } from '@wunderwerk/lru-map';

const map = new SizeBasedLRUMap<string, string>(100);

map.set('one', { value: 'value-one', size: 20 });
map.set('two', { value: 'value-two', size: 50 });
map.set('three', { value: 'value-three', size: 30 });

map.toString();     // -> "one < two < three"
map.size;           // -> 100

// By adding a new entry and therefore exceeding the map size of 100,
// the oldest entries are removed until there is room for the new entry.
// In our case, the two oldest entries are removed.
map.set('four', { value: 'value-four', size: 30 });

map.toString();     // -> "three < four"