yapool: Simple Object Pool
yapool is a minimalist JavaScript object pooling library, currently at version 1.0.0. It provides a basic linked-list implementation for managing reusable objects, focusing on extreme simplicity over feature richness. Unlike more comprehensive alternatives, yapool offers a 'dead-simple' API for adding and removing objects. Its primary differentiator is its minimal footprint and straightforward approach, making it suitable for scenarios where `n` (the number of objects in the pool) is small. Developers should be aware that operations like `remove` have `O(n)` complexity, which limits its applicability for very large lists. Given its simplicity and the project's age (last published in 2014), its release cadence is very stable or infrequent, suggesting a maintenance-only status rather than active feature development.
Common errors
-
TypeError: Pool is not a constructor
cause Attempting to use `new Pool()` after importing incorrectly from a CommonJS module in an ES Module context, or attempting to use a named import that doesn't exist.fixEnsure you are using the correct import syntax for a CommonJS default export in ESM: `import Pool from 'yapool';`. If in CommonJS, use `const Pool = require('yapool');`. -
Error: Cannot find module 'yapool'
cause The `yapool` package has not been installed, or the import/require path is incorrect.fixInstall the package using npm: `npm install yapool`. Verify the package name in your `package.json` and in your `import` or `require` statement.
Warnings
- gotcha The `remove` operation, and potentially `add` if it checks for existence, has `O(n)` time complexity. This makes `yapool` unsuitable for applications requiring high performance with very large object pools, as iterating through the linked list to find an object can become slow.
- gotcha yapool is a bare-bones implementation lacking common features found in more robust object pooling libraries. It does not provide mechanisms for creating new objects when the pool is empty (factory functions), limiting the pool to pre-existing objects. It also lacks features like maximum pool size, object lifecycle callbacks (e.g., `onReturn`, `onBorrow`), or health checks. This requires manual management of object creation and disposal.
- gotcha As a CommonJS-first package published in 2014, `yapool` may not fully support or be optimized for modern ES Module (ESM) environments and bundlers. While `import Pool from 'yapool'` typically works, named imports might fail, and bundlers might not be able to perform effective tree-shaking, potentially leading to larger bundle sizes if only parts of the module were theoretically used (though `yapool` is small).
Install
-
npm install yapool -
yarn add yapool -
pnpm add yapool
Imports
- Pool
const Pool = require('yapool') - Pool
import { Pool } from 'yapool'import Pool from 'yapool'
Quickstart
const Pool = require('yapool');
const myObjectPool = new Pool();
// Add objects to the pool
const obj1 = { id: 1, value: 'alpha' };
const obj2 = { id: 2, value: 'beta' };
myObjectPool.add(obj1);
myObjectPool.add(obj2);
console.log(`Pool length after adding: ${myObjectPool.length}`); // Expected: 2
// Remove an object from the pool
myObjectPool.remove(obj1);
console.log(`Pool length after removing obj1: ${myObjectPool.length}`); // Expected: 1
// Attempt to remove an object not in the pool (it will be a no-op)
const obj3 = { id: 3, value: 'gamma' };
myObjectPool.remove(obj3);
console.log(`Pool length after attempting to remove obj3: ${myObjectPool.length}`); // Expected: 1
// The remaining object in the pool is obj2
// (Note: yapool does not provide a way to 'get' an object, only add/remove)