Using string concatenation this way is usually a bad idea, especially if you don't know how many iterations you will be doing. Each time you concatenate a string, you reallocate the memory needed to match the new string, and you need to collect the old garbage (which may not even be done during the cycle for performance reasons)
var htmlBuffer = []; htmlBuffer.push('<select name="pagenum" id="pagenum" style="width:135px" onChange="setPageSearchValue(this.value)">'); for(i=1 ; i<=tot_pages ; i++) { if(i.toString() == document.frmlist.start.value) { htmlBuffer.push("<option value='"+i+"' 'selected' >"+i+"</option>"); } else { htmlBuffer.push("<option value='"+i+"'>"+i+"</option>"); } } htmlBuffer.push('</select>'); htmlC = htmlBuffer.join('\n');
The above will determine the array to which you click each "row". It will dynamically allocate the memory needed to expand the data, and finally you allocate 1 row for the total amount of data. It is much more efficient. I donโt know if this is a real problem in your case (since we donโt know what tot_pages is), but it would never be a bad idea to avoid string concatenations in loops.
jishi source share