2

I have been processing some client side data which may be up to 100MB in total. I have been using a global variable to store the data and the variable is declared at the top of my JS file:

var data = null;

Followed by the definition, there are some functions that load data to this variable, such as:

data = new Object();
data.array = [];
for (var i=0;i<10000;i++){
  data.array[i] = i;
}

Then some operations will replace the data.array with some new data. I found that if I just set null to data before loading the new data, the memory of the browser will growing very fast:

data = null;
data = something_new;
data.array = something_new;

So I tried to use delete before loading new data:

delete data.array;

Now I have a question: do I have to delete each of the element in data.array or can I just delete the whole array using delete data.array; ? Thanks!

3
  • 2
    Using delete is a bad idea though. If you have new data available - just overwrite it. Or nullify otherwise. Commented Oct 28, 2015 at 4:40
  • 1
    Why is using delete a bad idea???? Using delete data; is valid and removes data completely, Setting data to 'null' or 'undefined' still keeps a reference called 'data' referencing undefined or null thus taking up a little space (whatever space a reference actualy uses?). If you don't intend on using data anymore delete it from the scope using delete. Commented Oct 28, 2015 at 5:25
  • 1
    @Blindman67 groups.google.com/forum/#!topic/v8-users/zE4cOHBkAnY + google something about "hidden classes" PS: delete does not remove anything "from the scope". And, yes, you must be exceptionally precise when you're talking about the standards or abstract performance recommendations. Commented Oct 28, 2015 at 5:48

1 Answer 1

4

JavaScript has automatic memory management and garbage collection. If you get rid of all references to a piece of data, the memory will be reclaimed (and there is nothing else you can do about it).

delete data.array is roughly the same as data.array = null (it just also removes the property itself, not only its value).

delete in a loop on all array elements is a lot of work.

All three are unnecessary if you end up doing data = null or data = newData anyway.

The best thing you can do is to null out everything as soon as possible (i.e. before engaging on the calculation of newData).

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, the problem is I need to use a global variable to store the data because the data will be frequently used.
well, if you need the data, you need the data. If the data you need is too large, you need to rethink your design. Temporary memory allocation by calculations on the way should be reclaimed by the browser automatically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.