Bardelman
Bardelman

Reputation: 2298

Declaring functions and memory optimization in javascript

I'm declaring a function this way :

function clearScrollViewCont(elm,indx,array){

            elm.removeAllChildren();
        $.scrollView.scrollViewCont.remove(elm);
        elm = null;     
}

then i call it inside a forEach function :

$.scrollView.scrollViewCont.getchildren().forEach(clearScrollViewCont);

All the previous code is inside another big function so when this big function is removed from the memory all its content will be cleared as well but i don't know what could be the faster way to free memory. I'm used to null every variable which i don't need anymore (even it's written inside a big function) but now, i wonder about clearing memory from declared functions In my previous code, declaring the 'clearScrollViewCont' function that way won't let me to null it later. On the other side, if i declare it this way :

 var clearScrollViewCont =  function (elm,indx,array){

        elm.removeAllChildren();
        $.scrollView.scrollViewCont.remove(elm);
        elm = null;     
    }

i can null it later (simply by nulling the 'clearScrollViewCont' variable) but i don't know if this itself takes more memory space then the first way as now i'm declaring a variable in addition to the function.

What are really pros and cons of each way ?

Upvotes: 0

Views: 57

Answers (1)

snrlx
snrlx

Reputation: 5107

In my opinion both ways take up the same amount of space. It shouldn't really matter whether you declare a function like:

function clearScrollViewCont(elm,indx,array){}

or

var clearScrollViewCont = function(elm,indx,array){}

In both cases a Function object is created, which is callable by its name: clearScrollViewCont(...); Remember that JavaScript Functions are also objects: (Functions) Therefore both ways take up the same amount of space in my opinion.

Upvotes: 1

Related Questions