5

Given an element like a div in the page, and I add some object to it like this:

div.data=data;

Now, when I try to remove the divelement, is it necessary to set its data to null before remove it?


Update:

In fact, I fetch data from the server, then I will list it like this(before the list, I will remove the old ememetns:

var data=[{name:'xx'},{name:'yy'}...];

var resultDiv=document.getElementById("result");
resultDiv.innerHTML=""; //clear the old items
for(var i=0,len=data.length;i<len;i++){
    var div=document.createElement("div");
    div.data=data[i];
    resultDiv.appendChild(div);
}
....
icktoofay
  • 126,289
  • 21
  • 250
  • 231
hguser
  • 35,079
  • 54
  • 159
  • 293
  • Depends on what `data` is. Older browsers, especially IE, had memory leak problems if the data contained circular references to the div (which includes closure variables if you have functions!) – Bergi Jun 04 '13 at 01:55
  • JS runtime should use a garbage collector these days, so I'd say no. The problem is likely to be the other way around - any data you attach to DOM elements will stay in memory if not removed. – millimoose Jun 04 '13 at 01:56
  • @millimose: JS runtime has always used a garbage collector since the beginning (NS2.0). The problem is older versions of IE used a reference counting garbage collector which couldn't handle circular references. – slebetman Jun 04 '13 at 03:28
  • @slebetman: I could handle circular references, only not across the COM interface boundary. – Bergi Jun 04 '13 at 13:55
  • Potentially relevant: [What's wrong with adding properties to DOM Element objects?](http://stackoverflow.com/q/1915341) (or at least good to know). – Felix Kling Jun 04 '13 at 23:35

1 Answers1

-1

No. When you delete the div there is no element that has access to div.data. So its unreferenced memory and its the job of the garbage collector to clear it up.

However garbage collection kicks in at an undeterministic time. Also the act of garbage collection takes time.

basarat
  • 261,912
  • 58
  • 460
  • 511
  • Deleting the reference still doesn't start the garbage collector. Then `data` just would be not only inaccessible, but inaccessible twice. – Bergi Jun 04 '13 at 13:57