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 🙏

© 2026 – Pkg Stats / Ryan Hefner

resource-puddle

v0.1.0

Published

Reference counted resource management with batched garbage collection

Readme

resource-puddle

Reference counted resource management with batched garbage collection.

Open a resource by id and release it when you are done. A resource is opened once and shared between everyone who opens it — open and release are synchronous and just move a ref counter. Once no one is holding a resource any more it goes idle, and idle resources are garbage collected together on an interval (after staying idle for gcIdleTicks ticks).

npm install resource-puddle

Usage

const ResourcePuddle = require('resource-puddle')

const rooms = new ResourcePuddle({
  open: (id) => openMyThing(id), // open a new resource (sync)
  gc: async (resources) => {
    // close all the idle resources passed in
    for (const r of resources) await r.close()
  },
  gcInterval: 5000
})

const room = rooms.open('foo') // opens 'foo', refs = 1
const same = rooms.open('foo') // noop, same resource, refs = 2

rooms.release('foo') // refs = 1
rooms.release('foo') // refs = 0, goes idle and gets gc'ed on a later tick

await rooms.destroy() // stop the gc timer and gc everything that is left

API

const pool = new ResourcePuddle(options)

Options include:

{
  // Open a resource for an id. Called once per id, receives the extra args
  // passed to pool.open(id, ...args). Whatever it returns is the resource.
  // Synchronous - if your resource needs async setup, return something with
  // its own ready()/open() and await that yourself.
  open (id, ...args) {},
  // Close a batch of idle resources. Called on each gc tick with all the
  // resources that have been idle long enough to collect.
  gc (resources) {},
  // How often to run the garbage collector, in ms. Set to 0 to disable the
  // timer and only gc manually. Defaults to 5000.
  gcInterval: 5000,
  // How many consecutive gc ticks a resource must stay idle before it is
  // collected. Reset to 0 whenever it is reopened. Defaults to 4.
  gcIdleTicks: 4
}

const resource = pool.open(id, ...args)

Open a resource for id, incrementing its ref count, and return it synchronously. The first call for an id calls the open hook with (id, ...args); subsequent calls return the same resource and ignore the extra args. Opening an idle resource revives it and resets its idle counter.

pool.release(id)

Decrement the ref count for id. When it reaches zero the resource goes idle and becomes eligible for garbage collection once it has stayed idle for gcIdleTicks ticks.

Throws if the id is not open or has already been fully released.

await pool.gc()

Run the garbage collector now. Bumps the idle counter of every idle resource, collects the ones that have reached gcIdleTicks, removes them from the pool, and passes them to the gc hook as a single batch. Runs automatically every gcInterval ms.

pool.pauseGC()

Stop the automatic gc timer. Resources still ref count and go idle, they just won't be collected until gc is resumed (or gc() is called manually).

pool.resumeGC()

Restart the automatic gc timer after a pauseGC().

await pool.destroy()

Stop the gc timer and garbage collect everything that is left, regardless of ref count.

pool.size

Number of resources currently tracked.

pool.idle

Number of resources currently idle (released, awaiting collection).

License

Apache-2.0