There is no right answer, because there is no sample code in what it does.
However, the first thing you need to pay attention to is the DOM manipulation in loops. Whenever you touch the DOM in loops, this is usually a bad idea for performance, as manipulating the DOM is obviously slow.
Using JavaScript, you can significantly reduce DOM manipulation in loops by doing something like this (using jQuery in this example):
// make a container for your DOM additions var $div = $('<div>'), i, list = ['a','b','c','d'], for (i = 0; i < list.length; i++) { $div.append( $('<span>').text(list[i]) ); } // now you can append to the DOM once instead of in a loop $('body').append($div);
This, of course, is not necessarily related to loops. It can be any DOM manipulation called over and over, for example, window.resize or scrolling or moving the mouse or keyboard, etc. Etc. Inspect what your code does and identify the slowest parts. Start there.
source share