@stain-win/obj-pool

0.0.6 • Public • Published

ObjPool

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.

Features

  • 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.

Installation

You can install obj-pool using npm:

npm install @stain-win/obj-pool

Usage

Importing the Library

import { build, buildFactory, Poolable } from '@stain-win/objPool';

Creating a Pool

build<T>(Type: { new(): T }, reserve?: number, recycle?: (o: T) => T): Poolable<T>

Borrowing and Returning Objects

// 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);

Examples

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);

License

This project is licensed under the MIT License.

Acknowledgments

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.

Development

This workspace has been generated by Nx, a Smart, fast and extensible build system.

Running tasks

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.

Want better Editor Integration?

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.

Ready to deploy?

Just run nx build demoapp to build the application. The build artifacts will be stored in the dist/ directory, ready to be deployed.

Readme

Keywords

none

Package Sidebar

Install

npm i @stain-win/obj-pool

Weekly Downloads

1

Version

0.0.6

License

MIT

Unpacked Size

12 kB

Total Files

6

Last publish

Collaborators

  • stain-win