# Util

## pool

Object pooling is to cache instances or objects to be reused later\
This can be used to reduce object creation and increase performance (exponentially)

```luau
local pool = util.pool
local partpool = pool.new("void", 5, template):expand(20)
-- a part pool of 20 parts, and will create 5 parts everytime it runs out
```

`"void"` here is a preset function that create `template` and hides them in the void when cached\
`"adornment"` is another preset that creates adornments and caches/hides them with `.Visible`

<table><thead><tr><th width="327">Member</th><th></th></tr></thead><tbody><tr><td><code>.t</code></td><td>A table of preset functions</td></tr><tr><td><code>.new(string|function, number?, ...)</code></td><td>Creates a new pool using preset or function</td></tr></tbody></table>

```luau
local customObjectPool = pool.new(function(obj, taking)
    if obj then
        obj.Visible = taking -- show when object is used, hide when not
        return
		end
			
		-- if no obj then create and return
		return createObject()
end, 5):expand(10)
```

<table><thead><tr><th width="330">Method</th><th>Description</th></tr></thead><tbody><tr><td><code>:expand(number?): self</code></td><td>Creates more objects <code>(number | expansion | 1)</code></td></tr><tr><td><code>:give(object)</code></td><td>Puts <code>object</code> in the pool to be used later</td></tr><tr><td><code>:take(): object</code></td><td>Takes an object from the pool</td></tr><tr><td><code>:clear()</code></td><td>Clears all objects in the pool (destroys)</td></tr></tbody></table>

<table><thead><tr><th width="111">Property</th><th>Description</th></tr></thead><tbody><tr><td><code>.free</code></td><td>All free objects in the pool (ready to be used)</td></tr><tr><td><code>.busy</code></td><td>All the objects that were taken with <code>:take()</code> and not given back</td></tr><tr><td><code>.expansion</code></td><td>If <code>:take()</code> is called and there are no free objects, it will <code>:expand(expansion)</code></td></tr></tbody></table>
