Firefox throws js error for distribution overflow size loop

Below is my code

The same code works on the local server, but not live.

htmlC = ""; htmlC += '<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) { htmlC += "<option value='"+i+"' 'selected' >"+i+"</option>"; } else { htmlC += "<option value='"+i+"'>"+i+"</option>"; } } htmlC += '</select>'; 

I tried to find an infinite loop, but did not succeed. The very same code runs on the local server.

+5
source share
1 answer

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.

+15
source

Source: https://habr.com/ru/post/1209875/


All Articles