Skip to content

@web-engine-dev/math


@web-engine-dev/math / ObjectPool

Class: ObjectPool<T>

Object pool for reusing temporary math objects.

Helps reduce GC pressure in hot paths by reusing pre-allocated objects instead of creating new ones. Objects obtained from the pool should be returned when no longer needed.

Examples

ts
// Get a temporary vector from the pool
const temp = MathPool.getVec3();
temp.setMut(1, 2, 3);

// Use it for calculations...
const result = someOperation(temp);

// Return it to the pool when done
MathPool.releaseVec3(temp);
ts
// Scoped usage with automatic cleanup
MathPool.withVec3((temp1, temp2) => {
  Vec3.addOut(a, b, temp1);
  Vec3.scaleOut(temp1, 2, temp2);
  return temp2.length();
});

Type Parameters

T

T

Constructors

Constructor

new ObjectPool<T>(factory, reset?, maxSize?): ObjectPool<T>

Parameters

factory

() => T

reset?

(obj) => void

maxSize?

number = 64

Returns

ObjectPool<T>

Accessors

size

Get Signature

get size(): number

Returns the current number of objects in the pool.

Returns

number

Methods

clear()

clear(): void

Clears all objects from the pool.

Returns

void


get()

get(): T

Gets an object from the pool or creates a new one.

Returns

T


preallocate()

preallocate(count): void

Pre-allocates objects in the pool.

Parameters

count

number

Returns

void


release()

release(obj): void

Returns an object to the pool.

Parameters

obj

T

Returns

void

Proprietary software. All rights reserved.