A couple of years ago, I created an object pooling utility for a games project I was building in AS3. Since then, I’ve used it quite a few times, in order to speed up apps and improve resource management, easing the load on the garbage collector by reusing objects instead of recreating them.
While object pooling isn’t a magic bullet to speed up every use case, it works especially well on things that are heavy to continually construct and destroy. A good example is my History of the World project, which uses an object pool for item renderers, instead of creating and destroying them as you navigate around – press ALT+CTRL to bring up the resource debugger, which shows a little information on its usage.
I recently updated the utility, improving its performance, adding features and putting loads of unit tests around it. It’s now hosted it over at GitHub. Using it is a simple as:
var pool:LoanShark = new LoanShark(SomeClass);
var someInstance:SomeClass = pool.borrowObject();
// Instead of nullifying an object, check it back into the pool