A pool that hands out reusable objects and takes them back, avoiding repeated allocation.
npm install @tsdotnet/object-poolimport 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 backPast maxSize the pool trims itself, and autoTrim() schedules that.
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>