Object pools in high performance javascript?

前端 未结 5 1243
别跟我提以往
别跟我提以往 2021-01-29 21:53

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?<

5条回答
  •  抹茶落季
    2021-01-29 22:01

    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.

提交回复
热议问题