Self-invoking function + closure to improve hangs due to garbage collection

215 views Asked by At

I'm trying to determine whether this pattern will help minimize garbage collection in my Javascript game.

I have a function I call in my main game loop that is defined something like this:

my.object.prototype.method = function(some_object) {
    var a,b,c,d,e,f,j,k;

    // initialize variables    
    // do stuff to some_object based on variables...

    return some_object;
}

The variables end up being references to objects elsewhere in the code (or numeric values).

I'm trying to decrease the frequency and severity of garbage collection events since they're causing stutters in the frame-rate.

Would refactoring to a self-invoking function that stores the variables in a closure cause the variables to get re-used rather than re-allocated for each function call? Would it have any noticeable effect on GC?

my.object.prototype.method = (function() {
    var a,b,c,d,e,f,j,k;

    return function(some_object) {
        // initialize variables
        // do stuff to some_object based on variables...
        // set variables to undefined 

        return some_object;
    }
})();

Note, these are not objects being allocated in the method; just references to other objects or primitives.

I tried it out and it seems to have made things worse if anything. It seems GC is being done much more frequently and has about the same impact on performance each time (and is collecting about the same amount of memory as before).

Is there something I'm missing here that might be detracting from performance? Is this going to behave how expected (allocating variables once and re-using them without ever garbage collecting them?) or is there something else going on?

1

There are 1 answers

2
noneabove On

I'm not positive about this, but it might help if you cleared out the objects in code, with a wipe function. I found this interesting article that might help you more: https://www.scirra.com/blog/76/how-to-write-low-garbage-real-time-javascript

In particular I would look at this:

You can actually re-cycle an existing object (providing it has no prototype chain) by
deleting all of its properties, restoring it to an empty object like {}. For this you
can use the cr.wipe(obj) function, defined as:

    // remove all own properties on obj,
    effectively reverting it to a new object
    cr.wipe = function (obj)
    {
    for (var p in obj)
    {
        if (obj.hasOwnProperty(p))
            delete obj[p];
        }
    };

That or set them equal to nothing, for example if an array is used, set its length to 0, that way later it won't have to be garbage collected when you reassign it