Memory Leak In Javascript, Functions Inside Loop
I am looping over a response that I got from an ajax response, the response is an array containing 1000 objects , this response is used to created an html table with 1000 rows : 1s
Solution 1:
1000 table entries for modern web browsers should not cause any issue.
Here I'm adding 10,000 items into a table without ram problems. The reason why it's fast is because I build up the list inside a detached DOM element, then attach when done.
Your problem might be just down to DOM draw issue, the browser having to redraw on all your updates.
Another thing I noticed, for (var i in msg.myObjects)
depending on what your msg.myObjects contains this is not a good thing to use. If you can use modern JS,. for (const i of msg.myObjects)
is better.
var table = document.querySelector("table");
var tbody = document.createElement("tbody");
functionaddLine (txt) {
var tr = document.createElement("tr");
var td = document.createElement("td");
tr.appendChild(td);
td.innerText = txt;
tbody.appendChild(tr);
}
for (var l = 1; l <= 10000; l += 1) {
addLine("This is Line " + l + ", and some extra text");
}
table.appendChild(tbody);
<table><thead><tr><th>Test</th></tr></thead></table>
Post a Comment for "Memory Leak In Javascript, Functions Inside Loop"