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';
; let c = ;let first = c; // 1let second = c; // 2let third = c; // 3let fourth = c; // 4c; // truec; // trueconsole; // trueconsole; // trueconsole; // true // you can also explicitly set a specific number// and all subsequent allocations are still sequential// explicitly allocating number returns a "changed" booleanc; // truec; // false // you can check whether a number was allocated or notc; // false
There is also an alternate CounterImmutable
that gives a fully-persistent counter using structure sharing by path-copying strategy.
; // 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 cslet c = ;let first second third fourth;first c = c;second c = c;third c = c;fourth c = c; c = c; c = c;let first_ second_ fifth_;first_ c = c;second_ c = c;fifth_ c = c;console; // trueconsole; // trueconsole; // true // you can also perform a transactionc = c;
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