I'm writing some javascript code which needs to run fast, and uses a lot of short-lived objects. Am I better off using an object pool, or just creating objects as I need them?
I wrote a JSPerf test which suggests that there is no benefit to using an object pool, however I'm not sure if jsperf benchmarks are run long enough for the browser's garbage collector to kick in.
The code is part of a game, so I don't care about legacy browser support. My graphics engine won't work on old browsers anyway.
Source: Tips4all, CCNA FINAL EXAM
Object pools are used to avoid the instantiation cost of creating new objects by re-using existing ones. This is only going to be useful when the cost of instantiating the object is greater than the overhead incurred by using a pool.
ReplyDeleteWhat you've demonstrated is that very simple objects gain no benefit from pooling. As your objects become more complex this may change. My suggestion would be to follow the KISS principle and ignore object pooling until object creation has proved to be too slow.