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

weighted-lru-map

v1.0.2

Published

A LRU map supporting eviction based on calculated item weight

Downloads

2

Readme

weighted-lru-map

A LRU map supporting eviction based on calculated item weight in addition to the item count.

Introduction

This is an extension of the lru_map NPM module, adding support for cache eviction based on item weight, in addition to the limit on the item count. The weight of each item is calculated by a getWeight function supplied to the WeightedLruMap constructor.

This allows effecient LRU caching of items where the size varies significantly. With a traditional LRU map using only item count as the eviction criteria, adding 20 small items or 20 huge items would render the same number of evictions, making it hard to predict the RAM usage of a cache allowing 1 000 items of unknown size. With a WeightedLruMap, eviction can be set up to occur based on approximate memory usage, gaining more control over eviction and perhaps thereby allowing larger caches with better hit rate without risking out of memory.

Installation

$ npm install 'weighted-lru-map';

Examples:

Cache of arrays with length of each array as the weight

It's a good idea to avoid weights of zero, otherwise the map may theoretically contain an infinte number of empty arrays. For array elements this could be achieved using e.g. value.length + 1 or value.length || 1.

import {WeightedLruMap} from 'weighted-lru-map';

/**
 * Create a LRU map storing string arrays as values, 
 * where the item weight is the length of each array
 */
const map = new WeightedLruMap<number, string[]>(value => value.length || 1, 10);

map.set(1, ['A', 'B', 'C', 'D']); // weight is now 4
map.set(2, ['E', 'F']); // weight is now 6
map.set(3, ['G', 'H']); // weight is now 8
map.set(4, ['I', 'J', 'K', 'L']); // weight became 12, evicted item 1 to reach weight 8
map.set(5, []); // weight is now 9 (empty array has weight 1)
map.set(6, ['N', 'O', 'P', 'Q']); // weight became 13, evicted items 2 and 3 to 
                                  // reach weight 9

Cache of objects with the approximate memory usage as the weight

Using JSON representation used as the approximate size of objects:

import {WeightedLruMap} from 'weighted-lru-map';

/**
 * Create a LRU map storing arbitrary objects, where the item weight is the length of 
 * the JSON representation of each item
 */
const map = new WeightedLruMap<number, any>(value => JSON.stringify(value)?.length || 1, 50);

map.set(1, {foo: 42}); // weight is now 10 (length of '{"foo":42}')
map.set(2, {foo: 43}); // weight is now 20
map.set(3, {foo: 44}); // weight is now 30
map.set(4, {foo: 45, bar: 'awesome!', hello: 'world!'}); // weight became 74, evicting 
                                                         // items 1, 2 and 3 to reach weight 44
...