Hello Folks!
Ive recently read some Articles about Javascript/DOM GC CleanUp Problems when doing operations like
-
-
var Obj = createElement("TD");
-
Obj.onclick = function() { myFunc(); };
-
document.body.appendChild(Obj);
-
.....
-
document.body.removeChild(Obj); //<- Leak because JS function still set
-
I work right now on a Ajax Grid Class that can have different Events/Properties Attached
I have made the following Code right now to dynamicly remove Events and Objectreferences before removing the DOM Object
(The Sample here demonstrates how i remove entire <TR>'s with Content inside (taking gc Problem into account) / in this case all TR's of a given Table)
-
var Table = document.getElementById("myTable");
-
for (var iRow = Table.rows.length - 1; iRow >= 1; iRow--) {
-
var CurrentRow = Table.rows[iRow];
-
var Cell = CurrentRow.getElementsByTagName("TD");
-
for (var i=0; i < Cell.length; i++) {
-
var InputField = Cell[i];
-
for(var Event in InputField) {
-
//alert("Event:"+Event+" Value:"+InputField[Event]+" Typeof:"+typeof(InputField[Event]));
-
if (typeof(InputField[Event]) == "object") { InputField[Event] == null; }
-
if (typeof(InputField[Event]) == "function") { InputField[Event] = null; }
-
}
-
}
-
Table.deleteRow(iRow);
-
}
-
(I also null Objects because i use some Propeties to store Lookup Object References)
Note: Normaly i would just remove a single TR rather hole Content of a Table
To my Questions:
I suppose NULL'ing the Listener destroys the link in the same way as removeEventListener would!?
Is this correct?
My next question is, is the above Sample practicable or too many for loops/properties to work with?
I like it because its dynamically and doesnt seem to need extra attention
(also keep in mind that i would normaly only remove a single TR including its TD Content)
^Drawback it loops through really many unneeded Properties while processing
Or would it be better to make a Pseudo Class for each Grid that hold for every Event the informations and Update according?
But that means extra work also multiple Grids would mean multiple History->EventListeners
or is there a even better aproach?
Hope i can get with some of you people in touch about this ;-)