Git Product home page Git Product logo

obj-pool's Introduction

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.

obj-pool's People

Contributors

stain-win avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.