sea

An object pool focused on ease of use and resiliency

Usage no npm install needed!

<script type="module">
  import sea from 'https://cdn.skypack.dev/sea';
</script>

README

Sea

An object pool for node.js designed for ease of use and resiliency. Unlike many object pooling libraries, Sea has a bounded free list size while still keeping track of the count of outstanding objects. So, if you miss a free() in your code, the objects will be garbage collected as normal.

Example

var ObjectPool = require('sea'); // singleton

function Widget() {
    this.foo = null;
    this.bar = null;
}

// `clear` is required to be implemented on your type
Widget.prototype.clear = function clear() {
    this.foo = null;
    this.bar = null;
}

// `reset` is required to be implemented on your type
Widget.prototype.reset = function reset(foo, bar) {
    this.foo = foo;
    this.bar = bar;
}

ObjectPool.setup({
    Type: Widget,
    maxSize: 1000
});

function main(argv) {
    ObjectPool.bootstrap({
        statReceiver: function statReceiver(type, name, value, tags) {
            // type: 'gauge'
            // name: 'object-pool.free' or 'object-pool.outstanding'
            // value: int
            // tags: {name: 'poolname'}
            // do something with stat, like:
            statsd[type](name + '.' + tags.name, value);
        },

        reportInterval: 5000, // stat reporting interval
        timers: require('timers'), // or other timers object

        // true will enable the outstanding list to keep track of instances
        // that aren't getting freed
        debug: false
    });

    var w = Widget.alloc();
    w.reset('some', 'values');

    // do things with w

    w.free();

    ObjectPool.unref();
}

if (require.main === module) {
    main(process.argv);
}

License

MIT.