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

resource-counter

v1.2.4

Published

Sequentially Allocatable and Deallocatable Resource Counter

Downloads

2,013

Readme

js-resource-counter

Sequentially Allocatable and Deallocatable Resource Counter written in JavaScript. It is useful for tracking resource usage such as inodes and file descriptors. The resource counter is backed by a new lazy recursive perfectly balanced dynamically growing and shrinking fully-persistent bitmap tree data structure. This allows logarithmic allocation and deallocation performance. It's memory usage is better than the alternative deallocated stack + counter method.

Basic Usage

npm install --save 'resource-counter';
import Counter from 'resource-counter';

let c = new Counter;
let first = c.allocate(); // 1
let second = c.allocate(); // 2
let third = c.allocate(); // 3
let fourth = c.allocate(); // 4
c.deallocate(second); // true
c.deallocate(third); // true
console.log(c.allocate() === second); // true
console.log(c.allocate() === third); // true
console.log(c.allocate() === (fourth + 1)); // true

// you can also explicitly set a specific number
// and all subsequent allocations are still sequential
// explicitly allocating number returns a "changed" boolean
c.allocate(100); // true
c.allocate(100); // false

// you can check whether a number was allocated or not
c.check(100); // false

There is also an alternate CounterImmutable that gives a fully-persistent counter using structure sharing by path-copying strategy.

import { CounterImmutable } from 'resource-counter';

// c is being reassigned on every modification
// however each intermediate c can be used
// if you do use them, then you will now have diverging cs
let c = new CounterImmutable;
let first, second, third, fourth;
[first, c] = c.allocate();
[second, c] = c.allocate();
[third, c] = c.allocate();
[fourth, c] = c.allocate();
[, c] = c.deallocate(second);
[, c] = c.deallocate(third);
let first_, second_, fifth_;
[first_, c] = c.allocate();
[second_, c] = c.allocate();
[fifth_, c] = c.allocate();
console.log(first_ === second); // true
console.log(second_ === third); // true
console.log(fifth_ === (fourth + 1)); // true

// you can also perform a transaction
c = c.transaction((ct) => {
  const number = ct.allocate();
  ct.allocate();
  ct.deallocate(number);
  console.log(number); // 5
});

This can be useful if you need to combine CounterImmutable with other fully-persistent data structures to create composite data structures.

Documentation

Documentation is located in the doc folder. You can also view the rendered HTML.

Performance behaviour is lazy memory allocation on counter allocation (for both balanced tree growth and explicit counter allocation). This laziness means intermediate tree nodes won't be allocated when explicitly allocating a counter that has intermediate values. For example allocating only 0 and 500, tree nodes won't be created eagerly in anticipation for counter values 1 to 499.

By default both Counter and CounterImmutable will lazily grow and shrink the tree as necessary. However shrinking adds extra performance overhead for the benefit of more tighter memory usage. If you expect to always use the same set of numbers for allocation and deallocation, it will be faster if you disable shrinking. You can do this by passing the shrink parameter as false. See the documentation for more.

Development

To build this package for release:

npm run build

It will run tests, generate documentation and output multiple targets. One for browsers and one for nodejs. See rollup.config.js to see the target specification.

If your bundler is aware of the module field in package.json, you'll get the ES6 module directly.

Once you've updated the package run this:

npm version <update_type>
npm publish