simple-object-pool
Version:
Super lightweight implementation of factory-based object pool.\ There is no error handling if the same object is released into the pool to keep it fast.
66 lines (65 loc) • 1.72 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class ObjectPool {
factory;
pool = [];
length = 0;
/**
* Creates an object pool.
* @constructor
* @param {Factory<T>} factory The factory function to create new objects.
*/
constructor(factory) {
this.factory = factory;
}
/**
* Retrieves an object from the pool. Creates a new object if the pool is empty.
* @returns {T} The retrieved object.
*/
get() {
if (this.length > 0) {
return this.pool[--this.length];
}
return this.factory();
}
/**
* Releases an object back into the pool.
* @param {T} item The object to release.
*/
release(item) {
this.pool[this.length++] = item;
}
/**
* Releases multiple objects back into the pool.
* @param {T[]} items The objects to release.
*/
releaseMany(items) {
for (let i = 0; i < items.length; i++) {
this.pool[this.length++] = items[i];
}
}
/**
* Gets the current size of the pool.
* @returns {number} The size of the pool.
*/
size() {
return this.length;
}
/**
* Clears the pool by removing all objects from it.
*/
clear() {
this.pool.length = 0;
this.length = 0;
}
/**
* Shrinks the pool to the specified size if it's larger.
* @param {number} targetSize The desired size to shrink the pool to. Default is 10.
*/
shrink(targetSize = 10) {
if (targetSize >= 0 && this.length > targetSize) {
this.length = this.pool.length = targetSize;
}
}
}
exports.default = ObjectPool;