Object Pool Examples:

Java
Java
C#
C#
C++
C++
Python
Python
TypeScript
TypeScript
▸ Object Pool Quick Review

GameObject Pools in TypeScript

Video Games are resource intensive programs, so we need to have a few tricks up our sleeves in order to squeeze out all of the performance we can get! To make sure we're limiting the use of memory allocation and freeing (especially garbage collection in some languages), we can make use of Object Pools to make reusable resources. While we're going to focus on a game development scenario in this tutorial, the concept can be applied anywhere an object pool fits!

High Level Design:

<div style="width:100%; margin:auto;text-align:center;"> <img src="https://www.devmaking.com/img/topics/designpatterns/ObjectPoolGameObject.png" alt="object pool UML diagram" style="width:550px;max-width:95%;"> </div>

The GameObject Class

The GameObject class will have a supporting class called Vector3D to specify the position, rotation, and scale of the game object in the virtual space.

class Vector3D
{
   public x: number;
   public y: number;
   public z: number;

   constructor()
   {
       this.clear();
   }

   public clear()
   {
       this.x = 0.0;
       this.y = 0.0;
       this.z = 0.0;
   }
}

class GameObject
{
   public position: Vector3D;
   public rotation: Vector3D;
   public scale: Vector3D;

   constructor()
   {
       this.position = new Vector3D();
       this.rotation = new Vector3D();
       this.scale = new Vector3D();
   }

   public clear()
   {
       this.position.clear();
       this.rotation.clear();
       this.scale.clear();
   }
}

The GameObject Pool:

// Note:
/*
   Try implementing your own datastructure
   for the reserve and active lists!
   This way, you can guarantee the runtime complexity
   and have full control over your pool's efficiency.
*/
class GameObjectPool
{
   private activeList: Array&lt;GameObject&gt;;
   private reserveList: Array&lt;GameObject&gt;;

   private numberActive: number;
   private numberReserved: number;

   constructor(reserve: number = 5)
   {
       this.activeList = new Array&lt;GameObject&gt;();
       this.reserveList = new Array&lt;GameObject&gt;();

       this.numberActive = 0;
       this.numberReserved = 0;

       this.initializeReserve(reserve);
   }

   private initializeReserve(reserve: number)
   {
       for(let i = 0; i &lt; reserve; i++)
       {
           const gameObject = new GameObject();
           this.reserveList.push(gameObject);
       }
   }

   public getGameObject(): GameObject
   {
       if(this.numberReserved == 0)
       {
           this.reserveList.push(new GameObject());
           this.numberReserved++;
       }

       const gameObject = this.reserveList.pop();
       this.numberReserved--;

       this.activeList.push(gameObject);
       this.numberActive++;

       gameObject.clear();

       return gameObject;
   }

   public returnGameObject(gameObject: GameObject)
   {
       // Get the index of the gameObject in the active list:
       const index = this.activeList.indexOf(gameObject);
       if(index &gt;= 0)
       {
           // Splice the list around the element to remove.
           // Splice can be an expensive operation, which is why
           // I would use a custom collection in a real scenario:
           this.activeList.splice(index, 1);
           this.numberActive--;

           // Add it to the reserve:
           this.reserveList.push(gameObject);
           this.numberReserved++;
       }
   }
}

Find any bugs in the code? let us know!