Pool Class

Module: cc Parent Module: js

A fixed-length object pool designed for general type.
The implementation of this object pool is very simple, it can helps you to improve your game performance for objects which need frequent release and recreate operations

Examples
Example 1:

function Details () {
   this.uuidList = [];
};
Details.prototype.reset = function () {
   this.uuidList.length = 0;
};
Details.pool = new js.Pool(function (obj) {
   obj.reset();
}, 5);
Details.pool.get = function () {
   return this._get() || new Details();
};

var detail = Details.pool.get();
...
Details.pool.put(detail);

Example 2:

function Details (buffer) {
   this.uuidList = buffer;
};
...
Details.pool.get = function (buffer) {
   var cached = this._get();
   if (cached) {
       cached.uuidList = buffer;
       return cached;
   }
   else {
       return new Details(buffer);
   }
};

var detail = Details.pool.get( [] );
...

Index

Properties
  • count Number The current number of available objects, the default is 0, it will gradually increase with the recycle of the object,...
Methods
  • constructor Constructor for creating an object pool for the specific object type.
  • get Get and initialize an object from pool.
  • _get Get an object from pool, if no available object in the pool, null will be returned.
  • put Put an object into the pool.
  • resize Resize the pool.

Details

Properties

count

The current number of available objects, the default is 0, it will gradually increase with the recycle of the object, the maximum will not exceed the size specified when the constructor is called.

meta description
Type Number
Defined in cocos2d/core/platform/js.js:940

Methods

constructor

Constructor for creating an object pool for the specific object type. You can pass a callback argument for process the cleanup logic when the object is recycled.

meta description
Defined in cocos2d/core/platform/js.js:905
Parameters
  • cleanupFunc Function the callback method used to process the cleanup logic when the object is recycled.
  • size Number initializes the length of the array
get

Get and initialize an object from pool. This method defaults to null and requires the user to implement it.

meta description
Returns Object
Defined in cocos2d/core/platform/js.js:930
Parameters
  • params Any parameters to used to initialize the object
_get

Get an object from pool, if no available object in the pool, null will be returned.

meta description
Returns Object | Null
Defined in cocos2d/core/platform/js.js:950
put

Put an object into the pool.

meta description
Defined in cocos2d/core/platform/js.js:968
resize

Resize the pool.

meta description
Defined in cocos2d/core/platform/js.js:984

results matching ""

    No results matching ""