We have a widget system on the frontend of webmail that allows us to use a factory() method and a destroy() method to avoid memory leaks. I recently wrote a Menu widget that saves data about each item in a property on the element called “_item”. Since this is a custom property I don’t need to keep it once the menu is hidden, I want to remove it for memory leak purposes.
The following code works like a charm:
delete element._item;
Except in Internet Explorer. Apparently IE doesn’t like people deleting properties off of elements. So I came up with this solution:
try {
delete element._item;
} catch (e) {
element.removeAttribute('_item');
}
This works great without erroring, but I don’t know if it actually has the same memory implications. Does anyone know?
April 12th, 2007 at 6:24 pm
In some quick tests, Firefox shows almost no difference when adding / removing properties on 50,000 elements. Admittedly, this is based on Windows Task Manager, which isn’t über-reliable. Also, I only stored simple, 12-character strings (complex objects may be different).
Unfortunately, IE slows down exponentially when adding or removing properties from many elements at once. It takes .5 seconds to add a simple property to 1,000 elements… and 51 seconds for 10,000 divs.