Assertfail

Self and the gc

06 Feb 2014

For many projects I’ve been using the pattern:

    ns.CtorFunction = function(){
        var self = this;
        this.doStuff = function(){
            // do stuff and use self in order to get the object variables
        }
    }

Turns out that this pattern is good for small sites with jQuery (that makes use of this in other ways). For larger code bases where you have lots of code this construction sometimes makes it difficult for the garbage collector. If you have a large single page app, then this might be an issue.

What other patterns are available?

For a knockout js site you could use the pattern:

    ns.CtorFunction = function(){
        this.doStuff = function(){
            // do stuff and use this in order to get the object variables
        }
    }

Knockout sends the appropriate context to callbacks from the view (for instance the DOM click handlers). This makes the above construction work mostly fine. This depends of course of the scope of the knockout view (changed by “with”).

This might not be true for every app. My coworker Peter found that it depends on the amount of objects. It might be that the browser (chrome) have some limits to the amount of graph traversal that is allowed to identify entities to be garbage collected.

Tags


Comments

Do you want to send a comment or give me a hint about any issues with a blog post: Open up an issue on GitHub.

Do you want to fix an error or add a comment published on the blog? You can do a fork of this post and do a pull request on github.