Skip to main content

Object pools in high performance javascript?


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: Tips4allCCNA FINAL EXAM

Comments

  1. 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.

    What 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.

    ReplyDelete

Post a Comment

Popular posts from this blog

Wildcards in a hosts file

I want to setup my local development machine so that any requests for *.local are redirected to localhost . The idea is that as I develop multiple sites, I can just add vhosts to Apache called site1.local , site2.local etc, and have them all resolve to localhost , while Apache serves a different site accordingly.