I just cannot for the life of me figure out this memory leak in Internet Explorer.
insertTags
simple takes string str
and places each word within start and end tags for HTML (usually anchor tags). transliterate
is for arabic numbers, and replaces normal numbers 0-9 with a &#..n; XML identity for their arabic counterparts.
fragment = document.createDocumentFragment();
for (i = 0, e = response.verses.length; i < e; i++)
{
fragment.appendChild((function(){
p = document.createElement('p');
p.setAttribute('lang', (response.unicode) ? 'ar' : 'en');
p.innerHTML = ((response.unicode) ? (response.surah + ':' + (i+1)).transliterate() : response.surah + ':' + (i+1)) + ' ' + insertTags(response.verses[i], '<a href="#" onclick="window.popup(this);return false;" class="match">', '</a>');
try { return p } finally { p = null; }
})());
}
params[0].appendChild( fragment );
fragment = null;
I would love some links other than MSDN and about.com, because neither of them have sufficiently explained to me why my script leaks memory. I am sure this is the problem, because without it everything runs fast (but nothing displays).
I've read that doing a lot of DOM manipulations can be dangerous, but the for loops a max of 286 times (# of verses in surah 2, the longest surah in the Qur'an).
* Memory leaks in IE7 and IE8, not sure about 6, but works perfectly fine in Safari 4, FF 3.6, Opera 10.5, Chrome 5... *