Jump to content

Pooling JS objects


sbat
 Share

Recommended Posts

Can you please share your experience: if pooling/caching normal, non-DOM JavaScript objects worthed it in mobile browsers for your projects?

 

I am speaking about 2 object per second coming and dying, like explosion or new enemy (not 500-bullet-hell). 

 

I come from Flash and Java background, and recommendations there are different:

  • In Flash, it is advised to pool and thus minimize number of mark&sweep passes. 
  • In modern Java, it is advised not to interfere with Java GC. Fast dying object (like explosion) will be handled very efficiently, long living will suffer some performance hits as it is copied between GC generations. 

I will probably do some simple tests myself, but your experience will be really helpful! 

 

Link to comment
Share on other sites

I believe the theory is that most javascript garbage collectors have two generations of objects - those who don't live long enough are collected more frequently and efficiently, pretty much like Java.

 

In practice though, creating and deleting objects continuosly makes quite a big difference, and it's a good idea to avoid it when possible. But to be hoest, with just a couple of objects per frame I don't think it's going to make any real difference. Get a dozen objects, and it may start to appear in your profiling. With hundreds of objects per frame it's going to be very noticeable.

 

It also really depends on the size of these objects. If they're small (and all of a similar size), memory defragmentation doesn't happen frequently, and when it does happen it's pretty fast.

Link to comment
Share on other sites

HTML5 Rocks put out an article discussing Object Pooling that's a good read. Object pooling is generally helpful in programming languages that have poor GC (such as JavaScript) and can be more hurt than helpful in programming languages that have good GC (such as Java). But like any optimization technique, it depends on your application. Large objects with lots of processing can benefit more from an object pool than small objects with little processing. But as Gio said, it all depends on what you're doing in you're application.

 

Sometimes if you have a bunch of small objects of different types, you can create one object pool to handle all the different objects. This can be helpful because you don't have to create multiple object pools but still can gain the benefit of manual memory management. 

Link to comment
Share on other sites

  • 2 weeks later...

That HTML5 Rocks article is really good.

In a game I made 6 months ago, I started with an Object Pool for the bullets but removed it because I was not seeing any performance improvement.

I think I did it poorly and forgot to remove the reference to the object or something. I'll give it another go on later games.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...