The main problem here is that calling append() in a loop is costly and slow. To optimize it, append them all at once rather than one at a time in the loop as shown below. let listHtml = "";
$.each(myArray, function(i, item) {
listHtml += " " + item + " ";
});
$("#list").html(listHtml); In the above code, the list of all list items html is prepared within each loop and then updates the html at the end.