Skip to content

About

A flexible Object Pool that trims the pool down to the specified max size after a specified delay.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Repository files navigation

@tsdotnet/object-pool: Reuse, don’t reallocate. A pool for objects that are costly.

npm version GitHub license

A pool that hands out reusable objects and takes them back, avoiding repeated allocation.

Install

npm install @tsdotnet/object-pool

Usage

import ObjectPool from '@tsdotnet/object-pool';

const pool = new ObjectPool(() => new Uint8Array(1024), buf => buf.fill(0));

const buf = pool.take(); // a pooled one, or a new one
pool.give(buf);          // kept for reuse; cleared shortly after
pool.rent(b => b.set([1, 2, 3])); // take, use, give back

Past maxSize the pool trims itself, and autoTrim() schedules that.

Use it from

ESM (import ObjectPool from '@tsdotnet/object-pool';) or CommonJS (const ObjectPool = require('@tsdotnet/object-pool').default;).

In a browser, without a bundler
<script src="https://cdn.jsdelivr.net/npm/@tsdotnet/disposable@2/dist/umd/disposable.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tsdotnet/exceptions@1/dist/umd/exceptions.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tsdotnet/object-pool@2/dist/umd/object-pool.min.js"></script>
<script>
  const ObjectPool = tsdotnet.objectPool.default;
</script>

Docs

tsdotnet.github.io/object-pool

About

A flexible Object Pool that trims the pool down to the specified max size after a specified delay.

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages