Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Pool<T>

An object pool. Useful if your game creates lots of very small objects of the same type each frame, such as a path-finding algorithm.

NOTE: The returnObj() method may take linear time; it's much more efficient to use reset() if possible.

Type parameters

  • T

Hierarchy

  • Pool

Index

Constructors

constructor

  • new Pool(ctorFunc: object, initialSize?: number, growCount?: number): Pool
  • Creates an object pool.

    Parameters

    • ctorFunc: object

      The constructor function for T instances.

      • constructor: function
        • Returns __type

    • Optional initialSize: number

      The initial size of the pool; defaults to 20.

    • Optional growCount: number

      The amount to grow this pool by if too many objects are borrowed; defaults to 10.

    Returns Pool

Properties

Private _c

_c: any

Private _growCount

_growCount: any

Private _index

_index: any

Private _pool

_pool: any

borrowedCount

borrowedCount: number

Returns the number of currently-borrowed objects.

returns

The number of currently-borrowed objects.

length

length: number

Returns the total number of pooled objects, borrowed or otherwise. Only really useful for debugging purposes.

returns

The total number of objects in this pool.

Methods

borrowObj

  • borrowObj(): T
  • Gets an object from this pool.

    see

    returnObj

    see

    returnObj

    Returns T

    An object from this pool.

reset

  • reset(): void
  • Acts as if all objects have been returned to this pool. This method should be used if you're implementing an algorithm that uses an arbitrary number of objects, and just want to return them all when you are done.

    see

    returnObj

    Returns void

returnObj

  • returnObj(obj: T): boolean
  • Returns an object to this pool.

    see

    borrowObj

    see

    reset

    Parameters

    • obj: T

      The object to return.

    Returns boolean

    true, assuming the object was actually from this pool, and not previously returned. In other words, this method will only return false if you try to incorrectly return an object.

toString

  • toString(): string
  • Returns this object as a string. Useful for debugging.

    Returns string

    A string representation of this pool.

Generated using TypeDoc