Git Product home page Git Product logo

objectpool's Introduction

Object Object Pool

Build Status

A manual memory management tool for JavaScript.

Why?

Sometimes you have a lot of objects. Sometimes having those garbage collect causes some sawtooth ugliness ugly sawtooth memory allocation in chrome devtools

OK but why is this bad?

Garbage collection can be slow, especially if you're working with a lot of objects. Sometimes it is better to allocate a bunch of objects you need and just reuse them. see this dope article

API

The API for this is fairly simple and straight forward. You have an alloc() method, a free(object) method, and a collect() method available.

constructor

the constructor for an ObjectPool has an optional number of objects you can give it. If you pass a number, it will preallocate that many objects.

let pool = new ObjectPool();
console.log(pool.status.totalAllocated);
// 0

let poolPreAlloc = new ObjectPool(5);
console.log(pool.status.totalAllocated);
// 5

The following all assume you have created an object pool.

let pool = new ObjectPool();

alloc()

This will assign myObject to be {}. You can use this object as you wish. When you are done with this obect you can free it.

console.log(pool.status.totalAllocated)
// 0

let myObject = pool.alloc();
// {}
console.log(pool.status.totalAllocated)
// 1
console.log(pool.status.totalFree)
// 0

free()

When you are finished with an object, calling pool.free(object) will mark it as free to use again. When you do this, you should also mark your reference to the object as null.

let myObject = pool.alloc();
// {}
console.log(pool.status.totalAllocated)
// 1
console.log(pool.status.totalFree)
// 0
pool.free(myObject); // internally recycles object
myObject = null;
console.log(pool.status.totalAllocated)
// 1
console.log(pool.status.totalFree)
// 1

collect()

collect() will remove all unused objects from your pool. If you have allocated 5 objects, and one is in use, internally you will have one allocated object and zero free objects.

let objectOne = pool.alloc();
let objectTwo = pool.alloc();

console.log(pool.status.totalAllocated);
// 2
console.log(pool.status.totalFree);
// 0

pool.free(objectTwo); // internally recycles object
objectTwo = null;

console.log(pool.status.totalAllocated)
// 2
console.log(pool.status.totalFree)
// 1

pool.collect();

console.log(pool.status.totalAllocated)
// 1
console.log(pool.status.totalFree)
// 0

objectpool's People

Contributors

willsonsmith avatar

Stargazers

Yasir Alguwaifli avatar kev zettler avatar  avatar Zorg avatar

Watchers

James Cloos 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.