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

ref-count-cache

v0.1.0

Published

Caching for external resources that tracks which objects are currently referenced

Downloads

3

Readme

RefCountCache - Data structure for managing external resources

RefCountCache is a handy data structure for managing external resources, especially ones that have significant parse time or are too heavy to maintain in memory forever. Videos, buffered audio, or WebGL textures are all examples of objects that are best to keep around in memory as long as they're needed, but take up significant space.

RefCountCache can be seen as a key-value store that keeps track of which values are currently referenced in code, and eventually disposes of those that are no longer used. As long as there is at least one reference to a value, it is guaranteed to be found in the cache. When the number of external references drops to zero, it is not immediately deleted, but is placed in line to be disposed. This helps account for scenarios where an object frequently moves in and out of use. The queue has some maximum size, and if it grows too large, the oldest items in the queue will be removed from the cache.

When an item has zero references, it is guaranteed to be in line for disposal. If it is referenced again before it is removed entirely, it will be removed from the disposal queue. As long as there is at least one reference to an object, it will never be removed from the cache.

Your cache instance can be constructed with a custom cleanup method. This is useful for objects that need to be manually disposed, like WebGL resources. The cleanup method takes two arguments: the string path used to store / retrieve the object, and the actual value stored in the cache.

function cleanUpTexture(path, tex) {
  // called when removed from cache
  glContext.deleteTexture(tex);
}

const textureCache = new RefCountCache(cleanUpTexture);

const tex = glContext.createTexture();
textureCache.addEntry('myTexture', tex);

API

The constructor of RefCountCache takes two optional arguments. The first is the cleanup method, described above. A cleanup method takes two arguments: the path used to store the object, and the actual object stored in the cache. A RefCountCache does not need a cleanup method.

The second argument is an options object used to configure the cache. It currently supports the following keys:

  • queueSize: the maximum size of the ejection queue, defaults to 20