2

im sorry if this is a repost of an old topic, but i did search and coulnd't find a similar post.

this following piece of code an example of how im doing javascript inheritance in a project i am doing, now onfortunately, most of the end-users are on very low memory winXP+EI7 setups, so i am looking to save every bit of memmory that i can.

now my question is: do i actually save any memory by setting params = null in the end of the constructor, and should i do the same to the MyClass pointer after i have assigned the window.namespace.MyClass.MySubclass pointer to the same value?

(function (window, undefined) {

    var MySubClass = function (params) {

        this.elements = {
            // jQuery dom elements
        };

        this.vars = {
           // vars
        };

        this.controls = {
            // class instances
        };

        this.init(params);
        params = null; // cleanup
    };

    MySubClass.prototype = new namespace.MyClass.Base();

    MySubClass.prototype.init = function (params) {

        // do stuff

        params = null; // cleanup
    };


    MySubClass.prototype.add = function (item) {
        // adds item to an internal collection
    };

    window.namespace.MyClass.MySubClass = MySubClass;

})(window);
caroen
  • 53
  • 4

2 Answers2

0

I'm fairly sure you don't — the variable goes out of scope at the end of the function anyway. Of course, if someone has actually tested this objectively, any confirmation or refutation would be welcome.

Ps. Also, to quote Donald Knuth: premature optimization is the root of all evil. Even if the trick you used did have some effect, it would almost certainly not be worth it.

Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
0

Setting params = null probably wont make a huge difference but is not a bad practice.

You can use the tools mentioned here to find / cleanup memory leaks.

Community
  • 1
  • 1
Farhan Ahmad
  • 5,148
  • 6
  • 40
  • 69