obj-pool is a lightweight JavaScript library for implementing object pooling, a design pattern used to improve performance in applications that require the frequent creation and destruction of objects. Object pooling can significantly reduce memory allocation and garbage collection overhead, leading to better application performance and smoother user experiences. Created for personal project and forked to npm package.
- Efficient object reuse: obj-pool helps you reuse objects instead of creating new instances, reducing memory allocation and enhancing performance.
- Customizable pool size: Configure the maximum number of objects in the pool to match usage requirements.
- Simple API: obj-pool provides a simple and straightforward API for borrowing and returning objects from the pool.
You can install obj-pool using npm:
npm install @stain-win/obj-pool
Importing the Library
import { build, buildFactory, Poolable } from '@stain-win/objPool';
build<T>(Type: { new(): T }, reserve?: number, recycle?: (o: T) => T): Poolable<T>
// Borrow an object from the pool
const pooledObject: = pool.allocate();
// Use the object
// ...
pooledObject.changeSomething('test');
// Return the object to the pool when done
pool.release(pooledObject);
You can find more usage examples and integration scenarios in the demo app directory of this repository.
const poolDummy: Poolable<Dummy> = build(Dummy, 20, (dummy: Dummy) => {
dummy.setChangeMe(0);
return dummy;
});
const poolDummyFactory: Poolable<Dummy> = buildFactory(() => new Dummy(args));
const dummy: Dummy = poolDummy.allocate();
const dummyF: Dummy = poolDummyFacotry.allocate();
poolDummy.release(dummy);
poolDummyFactory.release(dummyF);
This project is licensed under the MIT License.
obj-pool was inspired by the concept of object pooling in game development and aims to bring similar performance benefits to a wide range of JavaScript applications. More optimizations coming soon.
✨ This workspace has been generated by Nx, a Smart, fast and extensible build system. ✨
To execute tasks with Nx use the following syntax:
nx <target> <project> <...options>
You can also run multiple targets:
nx run-many -t <target1> <target2>
..or add -p
to filter specific projects
nx run-many -t <target1> <target2> -p <proj1> <proj2>
Targets can be defined in the package.json
or projects.json
. Learn more in the docs.
Have a look at the Nx Console extensions. It provides autocomplete support, a UI for exploring and running tasks & generators, and more! Available for VSCode, IntelliJ and comes with a LSP for Vim users.
Just run nx build demoapp
to build the application. The build artifacts will be stored in the dist/
directory, ready to be deployed.